PHP网络商店购物车实时小计

发布于 2025-01-31 05:53:12 字数 2408 浏览 1 评论 0原文

我有一个像CART系统这样的网络商店,您可以将项目添加到购物车中,它们将出现在此处。我存储了产品的数量和价格。我也有一个+和 - 按钮,因此您可以将存储数量更改为1。只有一个函数丢失:当用户单击时,例如++<+<< /code>按钮,它将数量增加并立即将其存储在日期库中。但是可悲的是,它不会显示新的价格(将原始价格乘以新的数量值)。如果我重新加载页面,则价格将被乘以,但是在我单击+按钮后,我无法弄清楚如何立即显示新价格。我需要一些想法,您将如何做?

index.php

php:

$sql = "SELECT * FROM `table` WHEN 1";
$res = $con-> query($sql);
if ($res -> num_rows > 0) { $subtot = 0; // Subtotal variable to save the new subtotal
   while ($item = $res-> fetch_assoc()) {
      echo ''; // Display the items with their data using HTML
      // I also have the JS script here, so I can use the `+` and `-` button functions
      // I use ajax, and pass the product's id and the new quantity to a new .php file (for example up.php)
     // Add the values to the subtot varialbe
     $subtot += $item['price'] * $item['quantity']; // So on page refresh I get the item's price and value so I can multiply the price and show the subtotal.
     // I think somewhere here should be the code to update the `$subtotal` on the `+` button click to multiply the price with the new quantity, but I can't change php variables on js button clicks.
   }
}
echo $subtotal; // Display the subtotal

+按钮脚本:

document.getElementById("plusbutton").addEventListener("click", () => { if var bsi__data = new FormData(); bsi__data.append("pid", "'.$product['pid'].'"); bsi__data.append("quantity", document.getElementById("bspq__inp'.$product['pid'].'").value); bsi__data.append("price", "'.$product['product_price'].'");
$.ajax({
enctype: "multipart/form-data", type: "POST", url: "/assets/php/basket/q__up.php", data: bsi__data, dataType: "json", contentType: false, processData: false,
success: function(data) { console.log(data); su__refresh(); },
error: function (data) { console.log("error"); console.log(data); }
});
});

最后是up.php

if ($stmt = $con->prepare('UPDATE cart SET quantity = ? WHERE pid = ? AND uid = ?')) {
            $stmt->bind_param('iii', $_POST['quantity'], $_POST['pid'], $uid);$stmt->execute(); die('ok');
        } else { die ("error");

I have a webshop like cart system you can add items to the cart and they will appear there. I store the product's quantity and the price. I also have a + and - buttons so you can change the stored quantity by 1. Only one function missing: When the user clicks on for instance the + button, it incrises the quantity and stores it in the datebase immediately. BUT sadly it won't show the new price (multiply the original price with the new quantity value). If I reload the page, the price will be multiplied, but I can't figue out how to show the new price right after I click onto the + button. I need some idea how you would do this?

index.php

PHP:

$sql = "SELECT * FROM `table` WHEN 1";
$res = $con-> query($sql);
if ($res -> num_rows > 0) { $subtot = 0; // Subtotal variable to save the new subtotal
   while ($item = $res-> fetch_assoc()) {
      echo ''; // Display the items with their data using HTML
      // I also have the JS script here, so I can use the `+` and `-` button functions
      // I use ajax, and pass the product's id and the new quantity to a new .php file (for example up.php)
     // Add the values to the subtot varialbe
     $subtot += $item['price'] * $item['quantity']; // So on page refresh I get the item's price and value so I can multiply the price and show the subtotal.
     // I think somewhere here should be the code to update the `$subtotal` on the `+` button click to multiply the price with the new quantity, but I can't change php variables on js button clicks.
   }
}
echo $subtotal; // Display the subtotal

The + button script:

document.getElementById("plusbutton").addEventListener("click", () => { if var bsi__data = new FormData(); bsi__data.append("pid", "'.$product['pid'].'"); bsi__data.append("quantity", document.getElementById("bspq__inp'.$product['pid'].'").value); bsi__data.append("price", "'.$product['product_price'].'");
$.ajax({
enctype: "multipart/form-data", type: "POST", url: "/assets/php/basket/q__up.php", data: bsi__data, dataType: "json", contentType: false, processData: false,
success: function(data) { console.log(data); su__refresh(); },
error: function (data) { console.log("error"); console.log(data); }
});
});

And finally the up.php

if ($stmt = $con->prepare('UPDATE cart SET quantity = ? WHERE pid = ? AND uid = ?')) {
            $stmt->bind_param('iii', $_POST['quantity'], $_POST['pid'], $uid);$stmt->execute(); die('ok');
        } else { die ("error");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文