当我更改产品数量时,页面底部的总价不变。当我增加或减少数量时,总数必须自动更改。请帮助我
// Shopping cart page
<?php
include_once 'Dao/shoppingCartDao.php';
$shoppingCartItem = shoppingCartDao::getShoppingCartItems();
foreach ($shoppingCartItem as $item) {
?>
<tr>
<td><a href="detail.php?productId=<?php echo $item->getProductId(); ?>"><img src="./img/<?php echo $item->getProduct()->getImage(); ?>"></a></td>
<td><?php echo $item->getProduct()->getName(); ?></td>
<td>€ <?php echo $item->getTotalPriceExclVAT(); ?><small> (Excl. VAT)</small></td>
<td>€ <?php echo $item->getTotalPriceInclVAT(); ?><small> (Incl. VAT)</small></td>
<td>
<form action="" method="POST">
<input type="hidden" name="postcheck" value="true">
<input id="number" type="number" min="0" max="20" step="1" value ="<?php echo $item->getAantal(); ?>"/>
<input type="hidden" name="productId" value="<?php echo $item->getProductId() ?>">
<td><input type="submit" name="update" value="Update"></td>
</form>
<form action="deleteItem.php" method="POST">
<input type="hidden" name="postcheck" value="true">
<input type="hidden" name="productId" value="<?php echo $item->getProductId() ?>">
<td><input type="submit" name="deleteItem" value="Delete"></td>
</form>
</td>
</tr>
<?php
}
?>
<div>
<div>
<div><strong>Total Price (Excl. VAT)</strong></div>
<div><strong>Total Price (Incl. VAT)</strong></div>
<div><strong>VAT</strong></div>
<div>€ <?php echo shoppingCartDao::getTotalPriceExclVAT(); ?></div>
<div>€ <?php echo shoppingCartDao::getTotalPriceInclVAT(); ?></div>
<div>€ <?php echo shoppingCartDao::getTotalVAT(); ?></div>
</div>
</div>
答案 0 :(得分:1)
所以从评论中我推断出您想要以下内容:
要实现此目的,您应该创建一个专用的php脚本,该脚本会在服务器端更改金额并以新金额和所产生的价格做出响应。
updateQuantity.php
$productId = $_POST["productId"];
$quantity = $_POST["quantity"];
// Add error handling, authentication, ...
// ...
// Set the new quantity on server side
// This is just an example, the functions might not be existent!
$shoppingCartItem = shoppingCartDao::getShoppingCartItem($productId);
$success = $shoppingCartItem->setAantal($quantity);
$response = [
"success" => $success,
"productId" => $productId,
"quantity" => $shoppingCartItem->getAantal(),
"exclVAT" => $shoppingCartItem->getTotalPriceExclVAT(),
"inclVAT" => $shoppingCartItem->getTotalPriceInclVAT()
];
header('Content-Type: application/json');
echo json_encode($response);
然后,您可以从客户端网站上使用AJAX请求来更新数量。
首先,为您的电话号码输入ID或数据属性
echo '<input id="number" class="productQuantity" type="number" min="0" max="20" step="1" value ="'.$item->getAantal().'" data-product="'.$item->getProductId().'"/>';
,然后添加一个onChange侦听器(或满足您需要的任何侦听器):
$(".productQuantity").on("change", function() {
var productId = $(this).data("product");
var quantity = $(this).val();
$.ajax({
url: "updateQuantity.php",
data: {
productId: productId,
quantity: quantity
},
success: function(resp) {
// Maybe check if the resp.quantity is the same as the desired quantity, otherwise reset the input field
if (resp.success) {
// Seems to be successful...
// Update prices
$("...").text(resp.exclVAT);
$("...").text(resp.inclVAT);
}
}
});
});
这应该可以帮助您使其正常运行。不过,它不是可复制复制的代码。