简体   繁体   中英

How to insert data in laravel 5.2 using ajax serialize() function?

I am having a trouble implementing data insertion using laravel by passing data from my view using serialize() function to my controller.I am just starting to play around laravel but I am now stacked on this. Begging someone to help me solve this. Thanks a lot. Below are my codes.

Product Form

<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 Function when submit button is clicked

<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));


   }


 }

Model: Product.php

<?php

 namespace App\Product;

 use Illuminate\Database\Eloquent\Model;

 class Product extends Model
 {
   //
 }

Sample Input: 在此处输入图片说明

Error Output:

在此处输入图片说明

Well the issue is the csrf-token please user form helper class to declare your forms or declare the token. Else you will take millenniums to solve your problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM