如何使用ajax serialize()函数在laravel 5.2中插入数据?

时间:2016-03-05 10:11:29

标签: ajax postgresql laravel laravel-5.2

通过使用serialize()函数将数据从我的视图传递到我的控制器,我在使用laravel实现数据插入时遇到了麻烦。我刚开始玩laravel但我现在已经堆叠在此了。乞求别人帮我解决这个问题。非常感谢。以下是我的代码。

产品表单

<form class="form-horizontal prod-form" id="prod-form" style="background-color: #e2e2e2;" method="post" enctype="multiprodt/form-data">
          <fieldset>

            <div class="alert alert-dismissable alert-success alert-add-success">
              <button type="button" class="close" data-dismiss="alert">×</button>
              <center><h4>Data successfully saved.</h4></center>
            </div>

            <address></address>

           <input type="hidden" name="prod_id" class="prod_id" id="prod_id" value="">
           <input type="hidden" name="_token"  value="<?= csrf_token(); ?>">

            <div class="form-group">
              <label for="inputActivity" class="col-lg-2 control-label">Product Name</label>
              <div class="col-lg-10">
                <input type="text" class="form-control" name="prod[pharmaceutical]" id="inputPharmaceutical" placeholder="Product name" value="" style="width:260px;height:40px;" onchange="" required>
              </div>
            </div>

            <div class="form-group">
              <label for="inputActivity" class="col-lg-2 control-label">Description</label>
              <div class="col-lg-10">
                <input type="text" class="form-control" name="prod[description]" id="inputDescription" placeholder="Description" value="" style="width:260px;height:40px;" onchange="" required>
              </div>
            </div>

            <div class="form-group">
              <label for="inputActivity" class="col-lg-2 control-label">Unit</label>
              <div class="col-lg-10">
                <input type="text" class="form-control" name="prod[unit]" id="inputUnit" placeholder="Unit" value="" style="width:260px;height:40px;" onchange="" required>
              </div>
            </div>


            <div class="form-group">
              <label for="inputVenue" class="col-lg-2 control-label">Price</label>
              <div class="col-lg-10">
                <input type="text" class="form-control" name="prod[price]" id="inputPrice" placeholder="Price" value="" style="width:260px;height:40px;" required>
              </div>
            </div>

            <div class="form-group">
              <label for="inputSponsors" class="col-lg-2 control-label">Quantity</label>
              <div class="col-lg-10">
                <input type="text" class="form-control" name="prod[quantity]" id="inputQuantity" placeholder="Quantity" value="" style="width:260px;height:40px;" required>
              </div>
            </div>

            <div class="form-group">
              <label for="inputSponsors" class="col-lg-2 control-label">Amount</label>
              <div class="col-lg-10">
                <input type="text" class="form-control" name="prod[amount]" id="inputAmount" placeholder="Amount" value="" style="width:260px;height:40px;" required>
              </div>
            </div>

             <div class="form-group">
              <div class="col-lg-10 col-lg-offset-2">
                <button class="btn btn-primary submit-prod">Submit</button>
                <button class="btn btn-default">Cancel</button>
              </div>
            </div>

          </fieldset>
        </form>

单击提交按钮时的Javascript功能

<script type="text/javascript">

$(".submit-prod").click(function(e){

 e.preventDefault();

  var button_text = $(this).text();

   alert($("#prod-form").serialize());

   $.post("{{ url('/addprod') }}",$("#prod-form").serialize(),function(data){

       if(data.notify == "Success"){
         console.log(data.notify);
       }

     },"json");

   }); //end

 </script>

Route.php

 Route::group(['middleware' => 'web'], function () {

 Route::post('addprod', 'Product\ProductController@store');

 Route::get('/home', 'HomeController@index');

 });

ProductController.php

<?php

 namespace App\Http\Controllers\Product;

 use Illuminate\Http\Request;

 use App\Http\Requests;
 use App\Http\Controllers\Controller;
 use App\Product\Product as Product;

 class ProductController extends Controller
 {

/**
 * Show the application dashboard.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{
    return view('home');
}


public function create(){

}

public function store(Request $request){

    //$product = new Product;

    $prod_details = $request->all();

    $query = Product::create($prod_details);

      if($query){
        $notification = "Success";
      } else{
        $notification = "Failed";
      }

      echo json_encode(array('notify'=>$notification));


   }


 }

型号:Product.php

<?php

 namespace App\Product;

 use Illuminate\Database\Eloquent\Model;

 class Product extends Model
 {
   //
 }

示例输入: enter image description here

错误输出:

enter image description here

1 个答案:

答案 0 :(得分:1)

问题是csrf-token请用户表单助手类来声明表单或声明令牌。否则你将花费数千年来解决你的问题