使用窗体更新购物车中物料的数量


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() 是如何工作的?从提交(以及 if 子句)中删除product_id。添加一个隐藏的输入$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>