0

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: enter image description here

Error Output:

enter image description here

2
  • Are you uploading any file too? Commented Mar 5, 2016 at 13:29
  • No sir. Its just all user inputted text Commented Mar 5, 2016 at 13:45

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.