2017-08-31 8 views
0

カートにいくつかの数量を追加すると、カートに追加されたときにエラーが発生するのはなぜですか?ここに私のエラーです: TokenMismatchExceptionここTokenMismatchException数量を追加したときカートに追加

は私の見解である:

<div class="specs"> 
    <h4>Details</h4> 
    <p>{{ $product_info[0]->description }}</p> 
    <label>Quantity</label>           
    <div class="quantity"> 
     <form action="{{ route('prodcart',['id' => $product_info[0]->product_id ]) }}" method="post" id="frmaddcart"> 
      <input type="number" name="qty" min="1" max="{{ $product_info[0]->quantity }}" step="1" value="1" readonly> 
      {{ csrf_field() }} 
     </form> 
    </div> 
    <button id="btnaddCart" class="btn btn-success btn-cart"> ADD TO CART </button> 
</div> 

私のjQueryのsubmitボタン:私はすでにCSRF_field()も提出し、私のボタンを配置している私の見解で

$('#btnaddCart').click(function(){ 
    $('#frmaddcart').submit(); 
});  

、I既にjqueryを追加してコントローラに送信しました。ここで

は私のコントローラである。そして、

public function addToCart(Request $request, $product_id) 
    { 
     $quantity = $request->qty; 
     $product = DB::table('products') 
     ->select('*') 
     ->where(['products.product_id' => $product_id]) 
     ->first();   
     $oldCart = Session::has('cart') ? Session::get('cart') : null; 
     $cart = new Cart($oldCart); 
     $cart->add($product, $product->product_id, $quantity); 

     $request->session()->put('cart', $cart); 
     return redirect()->route('productlist',['id' => $product->subcategory_id ]); 
    } 

ここでは私のカートクラスです:

<?php 

namespace App; 

class Cart 
{ 
    public $items = null; 
    public $totalQty = 0; 
    public $totalPrice = 0; 

    public function __construct($oldCart) 
    { 
     if($oldCart) { 
      $this->items = $oldCart->items; 
      $this->totalQty = $oldCart->totalQty; 
      $this->totalPrice = $oldCart->totalPrice; 
     } 
    } 

    public function add($item, $product_id, $quantity) { 
     $storedItem = ['qty' => 0, 'price' => $item->price, 'item' => $item]; 
     if($this->items) { 
      if(array_key_exists($product_id, $this->items)) { 
       $storedItem = $this->items[$product_id]; 
      } 
     } 
     $storedItem['qty'] = $quantity; 
     $storedItem['price'] = $item->price * $storedItem['qty']; 
     $this->items[$product_id] = $storedItem; 
     $this->totalQty++; 
     $this->totalPrice += $item->price; 

    } 
} 
+0

私はこのページを送信するときに問題があると思います。ページの送信方法を共有しますか?あなたの返信のために – C2486

+0

@ user2486ありがとうございます。私のサブミットはボタンのid = "btnaddCart"にあり、そのjqueryの –

答えて

0

は、formタグの内側にボタンを提出しても、それはでsubmit

<div class="quantity"> 
    <form action="{{ route('prodcart',['id' => $product_info[0]->product_id ]) }}" method="post" id="frmaddcart"> 
     <input type="number" name="qty" min="1" max="{{ $product_info[0]->quantity }}" step="1" value="1" readonly> 
     {{ csrf_field() }} 
    <button type="submit" id="btnaddCart" class="btn btn-success btn-cart"> ADD TO CART </button> 
    </form> 
    </div> 

を入力作る置きますjqueryの場合、余分なパラメータを追加する必要があります_tokenとcsrfトークン値

var _token = $("input[name='_token']").val(); 
+0

私の質問が更新されました。私はあなたの提出が私の問題ではないと判断できるように、私のjqueryを投稿しました。ありがとう。レビューしてください。 –

0

更新 あなたがフォームを送信するためのjQueryを必要といけません。

  1. 次を削除します。

    $('#btnaddCart').click(function(){ 
        $('#frmaddcart').submit(); 
    }); 
    
  2. <form>SUBMIT BUTTON HTML HERE</form>

の内側にあなたの送信ボタンを移動

あなたは次のことを必要としないでください:

は一緒に送りますあなたのajaxとCSRFトークン平等。あなたのAjaxリクエストが以下のようになっているとします:

$.post(url, { 
    productId: productId, 
    qty: productQuantity, 
    _token: "{{ csrf_token() }}" // add this to send the token along with the request 
}); 
+0

お返事ありがとうございました。私の更新された質問を見てください。私はあなたの提出が私の問題であると判断できるように、私の送信ボタンのjqueryを入れます。私はなぜ私がTokenMismatchExceptionを得たのか分からない –

+0

私はすでにフォームに私のボタンの中に入れたが、まだエラー、私は2に量を追加する –

関連する問題