[英]TokenMismatchException in my add to cart when i added quantity
Why I am getting an error in my add to cart when I add some quantities in my cart. 当我在购物车中添加一些数量时,为什么我的购物车中出现错误。 Here is my error: TokenMismatchException
这是我的错误:TokenMismatchException
Here is my view: 这是我的看法:
<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>
my jquery submit button: 我的jQuery提交按钮:
$('#btnaddCart').click(function(){
$('#frmaddcart').submit();
});
in my view I have already put a CSRF_field()
also my submit button, I already added jquery to submit it in my controller. 在我看来,我已经放置了
CSRF_field()
以及我的提交按钮,我已经添加了jquery以将其提交到我的控制器中。
Here is my controller: 这是我的控制器:
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 ]);
}
Then here is my cart class: 这是我的购物车课程:
<?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;
}
}
Put submit button inside form tag and also make it type submit
将提交按钮放在表单标签内,并使其类型为
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>
In case of jquery you need to put extra param _token
with csrf token value 如果是jquery,则需要使用csrf令牌值放置额外的param
_token
var _token = $("input[name='_token']").val();
Update You dont need jQuery to submit your form. 更新您不需要jQuery来提交表单。
DELETE the following: 删除以下内容:
$('#btnaddCart').click(function(){ $('#frmaddcart').submit(); });
Move your submit button inside the <form>SUBMIT BUTTON HTML HERE</form>
将您的提交按钮
<form>SUBMIT BUTTON HTML HERE</form>
You Don't need the following: 您不需要以下内容:
Send along the CSRF token with your ajax request. 将CSRF令牌与ajax请求一起发送。 I assume that your ajax request looks something like below:
我假设您的ajax请求如下所示:
$.post(url, {
productId: productId,
qty: productQuantity,
_token: "{{ csrf_token() }}" // add this to send the token along with the request
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.