当我更改购物车中产品的数量时,总价不变(PHP)

时间:2019-05-21 09:02:16

标签: php html html-table

当我更改产品数量时,页面底部的总价不变。当我增加或减少数量时,总数必须自动更改。请帮助我

// 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>&euro; <?php echo $item->getTotalPriceExclVAT(); ?><small> (Excl. VAT)</small></td>
    <td>&euro; <?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>&euro; <?php echo shoppingCartDao::getTotalPriceExclVAT(); ?></div>
            <div>&euro; <?php echo shoppingCartDao::getTotalPriceInclVAT(); ?></div>
            <div>&euro; <?php echo shoppingCartDao::getTotalVAT(); ?></div>
      </div>
</div>

Explanation, using an image

1 个答案:

答案 0 :(得分:1)

所以从评论中我推断出您想要以下内容:

  • 您有一个购物车,用户可以在其中输入数字或单击输入数字的上/下箭头来更改每个商品的金额
  • 每当更改amont时,前端应进行更新,并且进一步,服务器应将新金额应用于购物车以供以后使用和/或结帐。
  • 您不希望用户每次输入新金额时都不必按下提交按钮或刷新页面。

要实现此目的,您应该创建一个专用的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);
            }
        }
    });
});

这应该可以帮助您使其正常运行。不过,它不是可复制复制的代码。