vendredi 27 juillet 2018

laravel 5.4 form submit using ajax

i am trying to form submit using jquery ajax

but getting POST https://kooziesicecream.com/cart/add 500 (Internal Server Error)

laravel form code

<form action="" method="post">

              

   <input type="hidden" name="img" value="{!! $product->product_image !!}" id="img{!! $product->product_image !!}">
    <input type="hidden" name="price" value="{!! $product->product_price !!}" id="price{!! $product->product_price !!}">
   <input type="hidden" name="name" value="{!! $product->product_name !!}" id="pro_name{!! $product->product_name !!}">
   <input type="hidden" name="id" value="{!! $product->id !!}" id="pro_id{!! $product->id !!}"><br>
              <!--<input id="form-control" type="number" max="10" min="1" class="form-control" name="qty" value="1">-->
              <div class="col-lg-1"></div>
              <div class="col-lg-3 col-xs-4">
                <select class="select_val" id="form-control{!! $product->id !!}" name="qty" >
                  <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>
                  <option value="10">10</option> 
                </select>
              </div>
              <div class="col-lg-3 col-xs-6">
                <button type="submit" class="btn btn-primary" id="addtocart<?php echo $product->id ?>">
                    <i class="fa fa-shopping-cart"></i> &nbsp;Add-to-cart
                </button>
              </div>
            </form>

cart controller

  public function addItem(Request $Request){

    $id   =  $Request->id;
    $name =  $Request->name;
    $price = $Request->price;
    $qty =   $Request->qty;
    $img = $Request->img;

    /*echo $qty."-price:".$id."-".$name."-".$price."-".$img   ; 
    die();
    $pro =  products::find($id);*/

    $cart = Cart::add(['id' => $id, 
               'name' => $name, 
                'qty' => $qty , 
                'price' => $price,
                'options'=>[
                   'img'=>$img
                ]

               ]);
    //dd($cart);
    //echo Cart::count();

   //$cart_total =  Cart::total();
      return response()->json(['success'=>'Data is successfully added']);
}

--jquery ajax---

jQuery(document).ready(function() {

    jQuery('[id^="addtocart"]').on('click',function(e){

        $.ajaxSetup({
              headers: {
                  'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
              }
        });
         e.preventDefault();
  var  pro_name =$(this).closest('form').find('[id^="pro_name"]').val();                                                                   

  var  qty =  $(this).closest('form').find('[id^="form-control"]').val(); 

  var  image  =  $(this).closest('form').find('[id^="img"]').val();

  var  price  =  $(this).closest('form').find('[id^="price"]').val();
        jQuery.ajax({
            url:' ',
            method: 'post',                 
            data : {
                    name: pro_name,
                    quantity : qty,
                    img : img,
                    price : price  
                },
                success  : function(data) {
                    console.log(data);
                }   
        }); 
  });    

});

i am trying to form submit using ajax jquery

How to get rid of this puzzle

please help me out

getting internal the server responded with a status of 500 (Internal Server Error)



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire