gpt4 book ai didi

javascript - Ajax 响应仅工作一次 - 使用 PHP 的购物车

转载 作者:行者123 更新时间:2023-12-02 17:01:09 25 4
gpt4 key购买 nike

我正在开发购物车。我首先将产品添加到购物车。然后使用 Ajax 更改项目数量。这个过程在 First Change 上完美运行。但是当我再次更改数量时,该部分没有刷新。

这是显示购物车详细信息的代码

        <?php
$session_id = $_SESSION["session_id"];
$query = "select c.cart_id, c.item_quantity, p.category_name, p.product_thumb, p.reduced_price from cart as c inner join category as p on c.category_id = p.category_id where c.session_id = '$session_id'";

$select_cart = mysqli_query($mysqli, $query);
if (mysqli_num_rows($select_cart) > 0)
{
?>
<table>
<tr>
<th>Item</th><th>Item Detail</th><th>Quantity</th><th>Price</th><th>Sub Total</th>
</tr>
<?php
while ($result_cart = mysqli_fetch_object($select_cart))
{
?>
<tr id="cart<?php echo $result_cart->cart_id; ?>">
<td width="100" align="center"><img src="images/products/<?php echo $result_cart->product_thumb; ?>" width="50" /></td>
<td><?php echo $result_cart->category_name; ?><br /><?php echo $result_cart->product_name; ?></td>

<td width="50" align="center">

<select class="quantity" id="quantity<?php echo $result_cart->cart_id; ?>" >
<?php

for ($i = 1; $i <= 10; $i++)
{

?>
<option value="<?php echo $i; ?>" <?php if ($result_cart->item_quantity == $i) { echo "selected"; } ?>><?php echo $i; ?></option>
<?php
}
?>

</select>

</td>

<td width="75" align="center">$<?php echo $result_cart->reduced_price; ?></td>
<td width="100" align="center">$<?php echo $result_cart->item_quantity * $result_cart->reduced_price; ?>
<a title="Delete" href="delete-cart.php?id=<?php echo $result_cart->cart_id; ?>" class="delete"></a></td>

</tr>
<?php
}
?>
</table>
<?php
}
else
{
?>
<p>Cart is Empty..!</p>
<?php
}
?>

这是在同一页面上使用的 Ajax 脚本

<script type="text/javascript">
$('.quantity').change( function()
{
var ID = $(this).attr("id");
var sid=ID.split("quantity");
var New_ID=sid[1];

var QTY = document.getElementById(ID).value;

var URL='updatecart.php';
var dataString = 'id=' + New_ID +'&qty='+ QTY;


$.ajax({
type: "GET",
url: URL,
data: dataString,
cache: false,
success: function(data){
$('#cart'+New_ID).html(data);
alert (data);

}
});
});
</script>

这是我将数据更新到数据库的代码 - updatecart.php

<?php
include('admin/includes/config.php');

if(isset($_GET['id'])) {

$cart_id = ($_GET['id']);
$item_quantity = ($_GET['qty']);

mysqli_query($mysqli, "update cart set item_quantity = '$item_quantity' where cart_id = '$cart_id'");

// Loop through the products and output HTML for JavaScript to use
?>
<?php
$query = "select c.cart_id, c.item_quantity, p.category_name, p.product_thumb, p.reduced_price from cart as c inner join category as p on c.category_id = p.category_id where c.cart_id = '$cart_id'";
$select_cart = mysqli_query($mysqli, $query);
$result_cart = mysqli_fetch_object($select_cart);
?>

<td width="100" align="center"><img src="images/products/<?php echo $result_cart->product_thumb; ?>" width="50" /></td>
<td><?php echo $result_cart->category_name; ?><br /><?php echo $result_cart->product_name; ?></td>

<td width="50" align="center">

<select class="quantity" id="quantity<?php echo $result_cart->cart_id; ?>" >
<?php

for ($i = 1; $i <= 10; $i++)
{

?>
<option value="<?php echo $i; ?>" <?php if ($result_cart->item_quantity == $i) { echo "selected"; } ?>><?php echo $i; ?></option>
<?php
}
?>

</select>

</td>

<td width="75" align="center">$<?php echo $result_cart->reduced_price; ?></td>
<td width="100" align="center">$<?php echo $result_cart->item_quantity * $result_cart->reduced_price; ?>
<a title="Delete" href="delete-cart.php?id=<?php echo $result_cart->cart_id; ?>" class="delete"></a></td>

<?php
}
?>

我需要在更改特定商品的数量时自动计算购物车的小计价格和总价格。我是 Ajax 新手。

这些是类似的问题。

1. Ajax form only work one time

2. Ajax Request works only once - code Ignitor

在引用这些问题后,我尝试使用类似的东西 $('.quantity').on( "change", "select", function() {但没有得到结果。

请帮助我。我需要更改整个编码结构吗?

最佳答案

您正在 ajax 成功后更改 DOM(使用 html() 方法)。该元素将被新元素替换,因此您的事件将被删除。

此外,您的另一个解决方案是仅对也已删除的数量元素应用更改。尝试类似的事情

$(document).on( "change", ".quantity", function() {}

关于javascript - Ajax 响应仅工作一次 - 使用 PHP 的购物车,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25710919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com