当我添加数量时,我添加到购物车中的TokenMismatchException

时间:2017-08-31 04:44:59

标签: php session laravel-5.4 cart

当我在购物车中添加一些数量时,为什么我在添加到购物车时收到错误消息。这是我的错误: 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提交按钮:

$('#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;

    }
}

2 个答案:

答案 0 :(得分: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();

答案 1 :(得分:0)

<强>更新 你不需要jQuery来提交你的表单。

  1. 删除以下内容:

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

  3. 中移动提交按钮

    您不需要以下内容:

    使用您的ajax请求发送CSRF令牌。我假设您的ajax请求如下所示:

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