简体   繁体   English

使用表单更新购物车中商品的数量

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

I have a shopping cart with a table that list all the items you have in it with quantities, price and product name. 我有一个带有桌子的购物车,其中列出了您的所有物品,包括数量,价格和产品名称。 Now I want users to be able to change the quantity of an item easily. 现在我希望用户能够轻松更改项目的数量。 How can I make this work? 我怎样才能做到这一点? This solution I made up myself doesn't appear to work: 我自己编写的这个解决方案似乎不起作用:

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

The function that actually does the updating works fine. 实际进行更新的功能正常。 It's just about how I make this form work. 这就是我如何使这个表格工作。

How does your updateCart() works? 您的updateCart()如何工作? Remove the product_id from your submit (and also in your if-clause). 从提交中删除product_id(以及在if子句中)。 Add an hidden input with you $item['product_id'] and call your updateCart() with those values. 使用$ item ['product_id']添加隐藏输入,并使用这些值调用updateCart()。

So it would be like 所以它会是这样的

<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