简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM