簡體   English   中英

使用表單更新購物車中商品的數量

[英]Updating quantity of an item in shopping cart with a form

我有一個帶有桌子的購物車,其中列出了您的所有物品,包括數量,價格和產品名稱。 現在我希望用戶能夠輕松更改項目的數量。 我怎樣才能做到這一點? 我自己編寫的這個解決方案似乎不起作用:

<tr>
        <td><?=$item["product_id"]?></td>
        <td>
            <form method="post" id="<?=$item["product_id"]?>">
                <input type="text" name="aantal" value="<?=$item["aantal"]?>">&nbsp;
                <input type="submit" name="change_aantal_<?=$item["product_id"]?>" value="Update">
            </form>
            <?  
                if(isset($_POST["change_aantal_".$item["product_id"].""])) {
                    updateCart($item["id"], $_POST["aantal"]);
                }
            ?>
        </td>
        <td><?=$item["aantal"] * $item["price"]?></td>
        <td><a href="/removefromcart.php?id=<?=$item["id"]?>">Verwijderen</a></td>
    </tr>

實際進行更新的功能正常。 這就是我如何使這個表格工作。

您的updateCart()如何工作? 從提交中刪除product_id(以及在if子句中)。 使用$ item ['product_id']添加隱藏輸入,並使用這些值調用updateCart()。

所以它會是這樣的

<table>
<tr>
    <td><?= $item["product_id"] ?></td>
    <td>
        <form method="post" id="<?= $item["product_id"] ?>">
            <input type="text" name="aantal" value="<?= $item["aantal"] ?>">&nbsp;
            <input type="submit" name="change_aantal" value="Update">
            <input type="hidden" name="product_id" value="<?= $item['product_id']; ?>">
        </form>
        <?
        if (isset($_POST["change_aantal"])) {
            updateCart($_POST["product_id"], $_POST["aantal"]);
        }
        ?>
    </td>
    <td><?= $item["aantal"] * $item["price"] ?></td>
    <td><a href="/removefromcart.php?id=<?= $item["product_id"] ?>">Verwijderen</a></td>
</tr>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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