2017-08-31 30 views
0

爲什麼我在我的購物車中添加一些數量時出現錯誤。這是我的錯誤: TokenMismatchExceptionTokenMismatchException在我添加到購物車時,我添加了數量

這是我的觀點:

<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的提交按鈕:

$('#btnaddCart').click(function(){ 
    $('#frmaddcart').submit(); 
});  
在我看來,我已經把 CSRF_field()也是我的提交按鈕

,我已經添加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

認沽提交按鈕表單標籤內,也使其鍵入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>

內按鈕你並不需要執行以下操作:

沿發CSRF令牌與您的ajax r eQUEST的。我假設你的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 –

相關問題