簡體   English   中英

更新 $_SESSION 產品數量未更新

[英]Update $_SESSION product quantity not updating

我正在嘗試更新購物車中的產品,但它不起作用.. 我不知道問題出在哪里。

<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'];
            }

        }
    }
}

這里的問題是當您使用<a>執行 GET 調用時,您沒有傳遞 POST 變量。 相反,嘗試使用 POST 變量對表單中的按鈕Update Item和字段“項目數”進行分組。 現在您只能通過 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM