简体   繁体   中英

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. Instead, try use POST vars grouping the buttom Update Item and field "number of items" inside a form. Now you can obtain values only via POST vars in PHP script, replacing where is $_GET by $_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'];
            }

        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM