我试图让使用PHP和AJAX的电子商务网站,但我在部分混淆。我在得到用户购物车的所有输入字段的值面临的问题。换句话说,假设某个用户在他们的车3项和每个项目的数量为“1”。现在,假设用户增加了第二个项目的数量在车并将其更改为“3”,并点击“更新购物车”按钮。当点击更新车,我要的是占用刚好在被更新的价值,而不是其他的。如果这是不可能的,我希望把所有的值,然后相应地更新数据库。目前,当我点击“更新购物车”按钮,它占用只有第一项的购物车中的数量值。
在PHP代码如下(请不介意使用的变量)
echo "<tr>
<td class='p-image'>
<a href='product-details?id=$fetchProductID'><img alt='' src='$theRealLink'></a>
</td>
<td class='p-name'><a href='product-details?id=$fetchProductID'>$theName</a></td>
<td class='p-amount'>INR $fetchProductUnitPrice</td>
<td class='p-quantity'><input maxlength='100' type='text' value='$fetchProductQuantity' name='quantity' class='productQuantity' data-quant='$fetchProductQuantity'></td>
<td class='p-total'><span>INR $fetchedProductTotal </span></td>
<td class='edit'><a href='javascript:void(0)' class='delete-from-cart' data-id='$fetchProductID'><img src='assets/img/icon/delte.png' alt=''></a>
</td>
</tr>";
而AJAX代码如下
$('#update-cart').click(function() {
var quantity = $('.productQuantity').val();
console.log(quantity);
});
我可能会去更新他们。您可以使用jQuery的为每个功能
$(".productQuantity").each(function() {
var quantity = $(this).val();
console.log(quantity);
// Get the data-id and build an array so you submit only 1 ajax request
});
我不知道如果我是对我的解释,但如果你要更新,使参考一购物车的商品数据库,这意味着你还需要确定哪些项目的值是一个被更新,但我不太看到,这是所提供的代码清晰。
无论如何,你可以在数量字段中添加的“的onkeyup”事件和标志设置到“项”,这一数量属于或将它储存物品更新的阵列上,一旦你点击“更新购物车”,然后只发送修改的项目到服务器。