我通过ajax传递一个id,由$ _SESSION删除。 ajax部分工作正常,php在POST中接收id,但vairable不能取消设置。为什么???这是我的代码:
ajax部分:
$(".delete").live('click', function(e){
e.preventDefault();
var id_to_remove = $(this).attr('id');
//alert(id_to_remove);
$.ajax({
type: "POST",
url: 'inc/functions/remove_item_from_cart.php',
data: { id : id_to_remove },
success: function(data) {
$("#content").load('inc/functions/get_checkout_content.php');
alert(data);
}
})
});
php接收部分:
session_start();
if(isset($_SESSION['cart']) && isset($_POST['id'])){
//echo var_dump($_SESSION['cart']);
$ncart=$_SESSION['cart'];
if (count($ncart)>0){
unset($ncart[$_POST['id']]); // this is NOT working!!!
$ncart=array_values($ncart);
$_SESSION['cart']=$ncart;
if(count($ncart)==0){
unset($_SESSION['cart']);
unset($_SESSION['cart_total']);
echo "all_empty";
} // this if part is the only working!
}
}
任何有用的建议为什么我不能取消设置会话变量?谢谢!
答案 0 :(得分:3)
我有自己的解决方案:
if (count($_SESSION['cart'])>0){
foreach ($_SESSION['cart'] as $key => $subarray){
if ($subarray['id'] == $_POST['id']){
unset($_SESSION['cart'][$key]);
break;
}
}
$_SESSION['cart'] = array_values($_SESSION['cart']);
} else {
if(count($_SESSION['cart'])==0){
unset($_SESSION['cart']);
unset($_SESSION['cart_total']);
echo "all_empty";
}
}
那是因为数组是这样的:
Array
(
[0] => Array
(
[id] => 3
[name] => Collier Ano petit
[price] => 45
[quant] => 1
[ptotal] => 45
)
)