[英]Update $_SESSION product quantity not updating
I'm trying to update a product in my cart but it won't work.. i don't know what the problem is.我正在尝试更新购物车中的产品,但它不起作用.. 我不知道问题出在哪里。
<div class="quantity">
<input class="input_display" name="aantal" type="number" value="<?=$value['item_quantity'];?>" min="1" max="<?php if($current_max_input_value == 0){ echo $max_input_number; }else{ echo $current_max_input_value; };?>">
</div>
<a href="?action=update&id=<?=$value['item_id'];?>" class="site-btn">Update Item</a>
if(isset($_GET['action'])){
if($_GET['action'] == 'update'){
foreach($_SESSION['shopping_cart'] as $key => $item){
//echo '<pre>';
//print_r($_SESSION['shopping_cart']);
//echo '<pre>';
if($item['item_id'] == $_POST['id']){
//UPDATE THE ITEM IN SHOPPING CART
$_SESSION['shopping_cart'][$key]['item_quantity'] = $_POST['aantal'];
}
}
}
}
The problem here is you don't pass POST vars when you use <a>
that performs a GET call.这里的问题是当您使用
<a>
执行 GET 调用时,您没有传递 POST 变量。 Instead, try use POST vars grouping the buttom Update Item
and field "number of items" inside a form.相反,尝试使用 POST 变量对表单中的按钮
Update Item
和字段“项目数”进行分组。 Now you can obtain values only via POST vars in PHP script, replacing where is $_GET by $_POST.现在您只能通过 PHP 脚本中的 POST 变量获取值,将 $_GET 替换为 $_POST。
<form action="cart.php" method="post">
<input type="hidden" name="action" value="update">
<input type="hidden" name="id" value="<?=$value['item_id'];?>">
<div class="quantity">
<input class="input_display" name="aantal" type="number" value="<?=$value['item_quantity'];?>" min="1" max="<?php if($current_max_input_value == 0){ echo $max_input_number; }else{ echo $current_max_input_value; };?>">
</div>
<button type="submit" class="site-btn">Update Item</a>
</form>
if(isset($_POST['action'])){
if($_POST['action'] == 'update'){
foreach($_SESSION['shopping_cart'] as $key => $item){
//echo '<pre>';
//print_r($_SESSION['shopping_cart']);
//echo '<pre>';
if($item['item_id'] == $_POST['id']){
//UPDATE THE ITEM IN SHOPPING CART
$_SESSION['shopping_cart'][$key]['item_quantity'] = $_POST['aantal'];
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.