在Laravel中使用Ajax表单保存数据

时间:2016-05-02 03:39:56

标签: php ajax laravel

我有这个表单,保存将使用Ajax。如果输入字段为空,则使用Ajax的表单会很好地验证。现在我的问题是如何将其保存到数据库中。

这是我的表格:

<form action="travelrequirementsadd" method="post" class="ajax-form">
  <div class="col col-clear-right">
    <label for="">Title<em>(required)</em></label>
    <input type="text" name="title" id="title" value="">
  </div>
  <div class="col col6 col-padright-10">
    <label for="">From<em>(required)</em></label>
    <input type="text" name="from" id="from" value="" placeholder="e.g Cebu">
  </div>
  <div class="col col6 col-padleft-10">
    <label for="">To<em>(required)</em></label>
    <input type="text" name="to" id="to" value="" placeholder="e.g Manila">
  </div>
  <div class="col col6 col-padright-10">
    <label for="">Departure<em>(required)</em></label>
    <div class="input-group date">
      <input type="text" name="departureDate" id="departureDate" class="form-control" placeholder="Select Departure Date">
      <span class="input-group-addon"><i class="glyphicon glyphicon-th"></i></span>
    </div>
  </div>
  <div class="col col6 col-padleft-10">
    <label for="">Return<em>(required)</em></label>
    <div class="input-group date">
      <input type="text" class="form-control" name="returnDate" id="returnDate" placeholder="Select Return Date">
      <span class="input-group-addon"><i class="glyphicon glyphicon-th"></i></span>
    </div>
  </div>
  <div class="col col4 col-padright-15">
    <label for="">Adults 12 + Yrs<em>(required)</em></label>
    <select class="form-control" name="adults" id="adults">
        <option value="Select">Select</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
        <option value="6">6</option>
        <option value="7">7</option>
        <option value="8">8</option>
        <option value="9">9</option>
    </select>
  </div>
  <div class="col col4 col-padright-5 col-padleft-5">
    <label for="">Children 2 - 12 Yrs<em>(required)</em></label>
    <select class="form-control" name="children" id="children">
        <option value="Select">Select</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
        <option value="6">6</option>
        <option value="7">7</option>
        <option value="8">8</option>
        <option value="9">9</option>
    </select>
  </div>
  <div class="col col4 col-padleft-15">
    <label for="">Infants 0 - 2 Yrs<em>(required)</em></label>
    <select class="form-control" name="infants" id="infants">
        <option value="Select">Select</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
        <option value="6">6</option>
        <option value="7">7</option>
        <option value="8">8</option>
        <option value="9">9</option>
    </select>
  </div>
  <div class="col col4 col-padright-15">
    <label for="sel1">Preferred Airline<em>(required)</em></label>
    <select class="form-control" id="sel1" name="preferredAirline" id="preferredAirline">
      <option value="Select">Any</option>
      <option value="1">Aer Lingus             </option>
      <option value="2">Aeroflot               </option>
      <option value="3">Aerolineas Argentinas  </option>
      <option value="4">Aeromexico</option>
      <option value="5">Air Canada</option>
      <option value="6">Air China</option>
      <option value="7">Air Europa             </option>
      <option value="8">Air France             </option>
      <option value="9">Air India              </option>
      <option value="10">Air India Express      </option>
      <option value="11">Air New Zealand        </option>
      <option value="12">Air Tahiti Nui         </option>
      <option value="13">Air Transat            </option>
      <option value="14">Air Vanuatu            </option>
      <option value="15">AirAsia                </option>
      <option value="16">AirAsia X              </option>
      <option value="17">Airberlin              </option>
      <option value="18">Alaska Airlines        </option>
      <option value="19">Alitalia               </option>
      <option value="20">Allegiant              </option>
      <option value="21">American Airlines      </option>
      <option value="22">ANA                    </option>
      <option value="23">Asiana                 </option>
      <option value="24">AtlasGlobal            </option>
      <option value="25">Austrian               </option>
      <option value="26">Avianca                </option>
      <option value="27">Bangkok Airways        </option>
      <option value="28">British Airways        </option>
      <option value="29">Brussels Airlines      </option>
      <option value="30">Cathay Pacific         </option>
      <option value="31">CEBU Pacific Air       </option>
      <option value="32">China Airlines         </option>
      <option value="33">China Eastern          </option>
      <option value="34">China Southern         </option>
      <option value="35">Condor                 </option>
      <option value="36">Copa Airlines          </option>
      <option value="37">Czech Airlines         </option>
      <option value="38">Delta                  </option>
      <option value="39">Dragonair              </option>
      <option value="40">EasyJet                </option>
      <option value="41">EL AL                  </option>
      <option value="42">Emirates               </option>
      <option value="43">Ethiopian Airlines     </option>
      <option value="44">Etihad                 </option>
      <option value="45">EVA Air                </option>
      <option value="46">Fiji Airways           </option>
      <option value="47">Finnair                </option>
      <option value="48">Frontier               </option>
      <option value="49">Garuda Indonesia       </option>
      <option value="50">Germanwings            </option>
      <option value="51">Gol                    </option>
      <option value="52">Gulf Air               </option>
      <option value="53">Hainan Airlines        </option>
      <option value="54">Iberia                 </option>
      <option value="55">Icelandair             </option>
      <option value="56">IndiGo Airlines        </option>
      <option value="57">Japan Airlines         </option>
      <option value="58">Jet Airways            </option>
      <option value="59">JetBlue                </option>
      <option value="60">Jetstar                </option>
      <option value="61">Kenya Airways          </option>
      <option value="62">KLM                    </option>
      <option value="63">Korean Air             </option>
      <option value="64">LAN Airlines           </option>
      <option value="65">Lion Airlines          </option>
      <option value="66">LOT Polish Airlines    </option>
      <option value="67">Lufthansa              </option>
      <option value="68">Malaysia Airlines      </option>
      <option value="69">Monarch                </option>
      <option value="70">Nok Air                </option>
      <option value="71">Norwegian              </option>
      <option value="72">Oman Air               </option>
      <option value="73">OpenSkies              </option>
      <option value="74">Peach                  </option>
      <option value="75">Philippine Airlines    </option>
      <option value="76">Porter                 </option>
      <option value="77">Qantas                 </option>
      <option value="78">Qatar Airways          </option>
      <option value="79">Regional Express       </option>
      <option value="80">Royal Brunei           </option>
      <option value="81">Royal Jordanian        </option>
      <option value="82">Ryanair                </option>
      <option value="83">S7 Airlines            </option>
      <option value="84">SAS                    </option>
      <option value="85">Saudia                 </option>
      <option value="86">Scoot Airlines         </option>
      <option value="87">Singapore Airlines     </option>
      <option value="88">Skylanes               </option>
      <option value="89">South African Airways  </option>
      <option value="90">Southwest              </option>
      <option value="91">SpiceJet               </option>
      <option value="92">Spirit                 </option>
      <option value="93">Spring Airlines        </option>
      <option value="94">Spring Japan           </option>
      <option value="95">SriLankan Airlines     </option>
      <option value="96">Sun Country            </option>
      <option value="97">SWISS                  </option>
      <option value="98">TACA                   </option>
      <option value="99">TAM                    </option>
      <option value="100">TAP Portugal           </option>
      <option value="101">THAI                   </option>
      <option value="102">Thomas Cook Airlines   </option>
      <option value="103">Thomson                </option>
      <option value="104">Tigerair               </option>
      <option value="105">Tuifly                 </option>
      <option value="106">Turkish Airlines       </option>
      <option value="107">United                 </option>
      <option value="108">Vanilla Air            </option>
      <option value="109">Virgin America         </option>
      <option value="110">Virgin Atlantic        </option>
      <option value="111">Virgin Australia       </option>
      <option value="112">Volaris                </option>
      <option value="113">Vueling Airlines       </option>
      <option value="114">WestJet                </option>
      <option value="115">Wizzair                </option>
      <option value="116">WOW air                </option>
     </select>
  </div>

  <div class="col col4 col-padleft-15">

    <label >Senior Citizen<em>(required)</em></label>
    <select name="seniorCitizen"  id="seniorCitizen"class="form-control">
         <option value="select">Select</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
        <option value="6">6</option>
        <option value="7">7</option>
        <option value="8">8</option>
    </select>
  </div>


  <div class="col col4 col-padright-5 col-padleft-5">
    <label for="">Visa needed<em>(required)</em></label>
    <select name="visaNeeded" id="visaNeeded" class="form-control">
        <option value="select">Select</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
        <option value="6">6</option>
        <option value="7">7</option>
        <option value="8">8</option>
        <option value="9">9</option>
    </select>
  </div>
  <div class="col col4 col-padright-5 col-padleft-5">
    <label for="">Passport needed<em>(required)</em></label>
    <select name="passportNeeded" id="passportNeeded" class="form-control">
        <option value="select">Select</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
        <option value="6">6</option>
        <option value="7">7</option>
        <option value="8">8</option>
        <option value="9">9</option>
    </select>
  </div>
  <div class="col col4 col-padleft-15">
    <label for="checktourpackage" >Travel requirements<em>(required)</em></label>
    <select name="travelRequirements" id="travelRequirements" class="form-control">
        <option value="select">Select</option>
        <option value="1">Tour only</option>
        <option value="2">Tickets only</option>
        <option value="3">Tour and Tickets</option>

    </select>
  </div>
  <div class="col col4 col-padleft-5">
    <label for="checktourpackage" >Hotel<em>(required)</em></label>
    <select name="hotel" id="hotel" class="form-control">
        <option value="select">Select</option>
        <option value="1">Yes</option>
        <option value="2">No</option>

    </select>
  </div>

  <div class="col">
    <label for="">Other travel details<em>(required)</em></label>
    <textarea name="otherTravelDetails" id="otherTravelDetails"></textarea>
  </div>
  <div class="post-quotation-footer">
    <a href="#" class="btn btn-default">Cancel</a>
    <input type="submit" value="Post Travel Requirements" class="btn btn-success" />
  </div>
  </form>

我使用class="ajax-form"调用Ajax函数进行验证并保存。

这是我的代码:

//travel requirements add
function ajax_travelrequirementsadd(){

    if (Auth::guest()) exit;

    $title  = $_POST['title'];
    $from   = $_POST['from'];
    $to     = $_POST['to'];
    $departureDate  = $_POST['departureDate'];
    $returnDate     = $_POST['returnDate'];
    $adults     = $_POST['adults'];
    $children   = $_POST['children'];
    $infants    = $_POST['infants'];
    $preferredAirline   = $_POST['preferredAirline'];
    $seniorCitizen  = $_POST['seniorCitizen'];
    $visaNeeded = $_POST['visaNeeded'];
    $passportNeeded   = $_POST['passportNeeded'];
    $travelRequirements = $_POST['travelRequirements'];
    $hotel  =   $_POST['hotel'];
    $otherTravelDetails = $_POST['otherTravelDetails'];


    //$user = User::find(Auth::user()->id);
    //$travelForm = TravelRequirement::find(Auth::user()->id);

    $data  = array(
                    'title' => $title,
                    'from'  => $from,
                    'to'    => $to,
                    'departureDate'=>$departureDate,
                    'returnDate' =>$returnDate,
                    'adults'  =>$adults,
                    'children'=>$children,
                    'infants'=>$infants,
                    'preferredAirline'=>$preferredAirline,
                    'seniorCitizen'=>$seniorCitizen,
                    'visaNeeded'=>$visaNeeded,
                    'passportNeeded'=>$passportNeeded,
                    'travelRequirements'=>$travelRequirements,
                    'hotel'=>$hotel,
                    'otherTravelDetails'=>$otherTravelDetails
                   );
    $rules = array(
                    'title' => "required",
                    'from'  => "required",
                    'to'    => "required",
                    'departureDate' => "required",
                    'returnDate'    => "required",
                    'adults'    => "required|Numeric",
                    'children'  => "required|Numeric",
                    'infants'   => "required|Numeric",                
                    'preferredAirline'=>"required|Numeric",
                    'seniorCitizen'=>"required|Numeric",
                    'visaNeeded'=>"required|Numeric",
                    'passportNeeded'=>"required|Numeric",
                    'travelRequirements'=>"required|Numeric",
                    'hotel'=>"required|Numeric",
                    'otherTravelDetails'=>"required"
                    );

    $validator = Validator::make($data, $rules);


    if($validator->passes()){
        //$travelForm  = new TravelRequirements();
        //$travelForm->save();

    }else{
        json_message($validator->errors()->toArray(), false);
    }
}

有人可以帮助我如何保存到数据库中? 任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:2)

一种方式是

if($validator->passes()){
     TravelRequirement::create($data);
}

另一种方式是

if($validator->passes()){

    $travelRequirement = new TravelRequirement;
    $travelRequirement->title =  $title;
    $travelRequirement->from  =  $from;
    $travelRequirement->to    =  $to;
    $travelRequirement->departureDate= $departureDate;
    $travelRequirement->returnDate = $returnDate;
    $travelRequirement->adults  = $adults;
    $travelRequirement->children= $children;
    $travelRequirement->infants= $infants;
    $travelRequirement->preferredAirline= $preferredAirline;
    $travelRequirement->seniorCitizen= $seniorCitizen;
    $travelRequirement->visaNeeded= $visaNeeded;
    $travelRequirement->passportNeeded= $passportNeeded;
    $travelRequirement->travelRequirements= $travelRequirements;
    $travelRequirement->hotel= $hotel;
    $travelRequirement->otherTravelDetails= $otherTravelDetails;
    $travelRequirement->save();
}