【发布时间】:2016-12-16 03:09:54
【问题描述】:
我写了一个从购物车中删除商品的函数;成功消息闪烁,但该项目没有被删除。控制台中没有标记错误消息。但是我认为我的查询是错误的。
cart.php
<form id='updateCartForm' action="update_cart.php" method="get">
<input name="cart_item_id" type = "hidden" id ="cart_item_id" value='<?=$product['id'];?>'>
</form>
<button class="btn btn-warning" onclick='update_cart(); return false;'>×</button>
footer.php
function update_cart(){
jQuery('#updateCartErrors').html("");
var cart_item_id = jQuery('#cart_item_id').val();
var error = ' ';
var data = jQuery('#updateCartForm').serialize();
jQuery.ajax({
url : '/ecommerce/customer/parsers/update_cart.php ',
method: 'get',
data : data,
success : function (){
location.reload();
},
error : function(){alert("Something went wrong");}
});
}
update_cart.php
<?php
ob_start();
require_once $_SERVER['DOCUMENT_ROOT'].'/ecommerce/core/init.php';
$cart_item_id = $_GET['cart_item_id'];
$sql = "DELETE FROM cart WHERE id = $cart_item_id" ;
//flash success message
$domain =($_SERVER['HTTP_HOST'] != 'localhost')?'.'.$_SERVER['HTTP_HOST']:false;
$_SESSION['success_flash'] = $product['prod_name']. ' was deleted from your cart.';
【问题讨论】:
-
@e4c5: 是的
$product['id']正在由另一个查询生成以显示购物车中的产品。 -
您是否检查过该表单是否正确呈现但检查了 html
-
页面中有多少个
<form id='updateCartForm'..?页面中是否可能有多个id ="cart_item_id"? -
@Sean:我的页面上只有一个表单。但是,如果这实际上可以帮助
$_SESSION['success_flash'] = $product['prod_name']. ' was deleted from your cart.';显示错误的产品名称,它会显示购物车上的第一个产品。所以说我在购物车上有一个t-shirt、一个hat和一个ring,无论我删除帽子还是戒指,success_flasht-shirt was deleted from cart -
您的闪存错误只是因为您从购物车中选择了第一行/第一行,而不是您删除的行。
标签: javascript php jquery mysql sql-delete