Php 使用表单更新购物车中项目的数量
我有一个购物车,上面有一张桌子,上面列出了你所有的物品,包括数量、价格和产品名称。现在,我希望用户能够轻松地更改项目的数量。我怎样才能做到这一点?我自己制定的解决方案似乎不起作用:Php 使用表单更新购物车中项目的数量,php,mysql,forms,Php,Mysql,Forms,我有一个购物车,上面有一张桌子,上面列出了你所有的物品,包括数量、价格和产品名称。现在,我希望用户能够轻松地更改项目的数量。我怎样才能做到这一点?我自己制定的解决方案似乎不起作用: <tr> <td><?=$item["product_id"]?></td> <td> <form method="post" id="<?=$item["product_id"]?>
<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>
updateCart()是如何工作的?
从提交中(以及在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"] ?>">
<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>
您是否考虑过JavaScript或jQuery?有一个onchange事件,当输入改变时触发。你可以在其中添加更新方法,你的工作就完成了。我如何在更改时执行PHP函数?哦,你想要PHP。。。我很抱歉,我没注意到。但是我不知道真正的问题在哪里。是否执行了函数even updateCart()?如果不是,这可能是你的If子句的问题,这里的$u帖子看起来非常奇怪。您没有在元素中指定操作。如果您想将其发送到自身,则它是有效的,但它不符合W3C标准。