Reputation: 543
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"]?>">
<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.
Upvotes: 0
Views: 5271
Reputation: 159
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"] ?>">
<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>
Upvotes: 2