dongzhao1865 2013-11-12 13:31
浏览 66
已采纳

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

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.

  • 写回答

1条回答 默认 最新

  • drau67562 2013-11-12 14:06
    关注

    How does your updateCart() works? Remove the product_id from your submit (and also in your if-clause). Add an hidden input with you $item['product_id'] and call your updateCart() with those values.

    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>
    

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料