我创建了一个购物车脚本,以便使用PHP和MySQL添加或删除产品。 该脚本按预期工作,但它不是很舒服。我试图将它与ajax调用一起使用。
1。我在购物车中添加了所选产品
$('#add-product').on('click',function() {
$.ajax({
var product_id = $(this).data('id'); //Product id
url: 'cart.php',
data:{add:product_id },
type:'GET',
success: function(data) {
if(!data.error){
$('#show-cart').html(data);
}
}
});
});
2。 cart.php
<?php
require_once('resource/init.php');
//ADD A PRODUCT IN THE SHOPPING CART
if(isset($_GET['add'])) {
$query = query("SELECT * FROM products WHERE id = ".escape_string($_GET['add']));
confirm($query);
while ($row = fetch_array($query)) {
if($row['quantity'] != $_SESSION['product_'.$_GET['add']]){
$_SESSION['product_'.$_GET['add']] +=1;
}
}
}
//DELETE PRODUCT FROM SHOPPING CART
if(isset($_GET['delete'])) {
$_SESSION['product_'.$_GET['delete']] = '0';
unset($_SESSION['total']);
unset($_SESSION['quantity']);
}
$total = 0;
$item_quantity = 0;
$item_name = 1;
$item_number = 1;
$amount = 1;
$quantity = 1;
foreach ($_SESSION as $name => $value) {
if(substr($name, 0,8) == 'product_' && $value > 0){
$lenght =strlen($name);
$id = substr($name, 8, $lenght);
$query = query("SELECT * FROM products WHERE id = ".escape_string($id)." ");
confirm($query);
while($row = fetch_array($query)) {
$sub_price = pr_get_sub_price($row['price'],$row['discount'],$row['id'],$value,$lang);
$item_quantity += $value;
$product_image = 'images/products/'.$row['image'];
$price = show_product_price($row['price'],$row['discount'],$lang);
$discounted_price = get_product_price_by_discount($row['price'],$row['discount'],$row['id'],$lang);
?>
<!--THIS IS THE SHOPPING CART-->
<table>
<tr style="text-align:left">
<td><a href="#"><img src="<?php echo $product_image;?>" alt="cart-hover" style="float:left;width: 58px; margin-right:14px;"/></a></td>
<td><a title="<?php echo $row['title'];?>" href="#" style="margin-right:10px;"><span class="cart-title"><?php echo $row['title'];?> (<?php echo $value;?>)</span></a></td>
<td><span style="font-size: 90%; margin-right: 8px; position:relative;top:2px;"><?php echo $price;?> <?php echo $discounted_price;?></span></td>
<!--DELETE BUTTON-->
<td>
<button data-id = "<?php echo $row['id'];?>" class="delete-product"> <i class="fa fa-close"></i> Delete</button></td>
</tr>
</table>
<input type="hidden" name="item_name_<?php echo $item_name;?>" value="<?php echo $row['title'];?>">
<input type="hidden" name="item_number_<?php echo $item_number;?>" value="<?php echo $row['id'];?>">
<input type="hidden" name="amount_<?php echo $amount;?>" value="<?php echo $row['price'];?>">
<input type="hidden" name="quantity_<?php echo $quantity;?>" value="<?php echo $value;?>">
<?php
$item_name++;
$item_number++;
$amount++;
$quantity++;
$_SESSION['total'] = $total += $sub_price;
$_SESSION['quantity'] = $item_quantity;
}
}
}
?>
<!--DELETE PRODUCT-->
<script>
$('.delete-product').click(function() {
var product_id = $(this).data('id');//PRODUCT ID
$.ajax({
url: 'cart.php?delete=' + product_id,
type: 'get',
success:function(data){
$('#show-cart').html(data);
}
});
});
</script>
上述脚本按预期工作,但不适用于DELETE操作。
如果我有购物车中的产品: 产品一(3)$ 250.00
然后点击DELETE图标,所选产品继续显示在购物车中,因为它在开始之前点击DELETE图标:产品一(3)$ 250.00
如果我尝试在购物车中添加相同的产品,则会以值1开头显示:产品一(1)$ 250.00
这是我第一次使用PHP / MySQL和Ajax,我不知道如何解决这个问题。