繁体   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