取消设置PHP的会话变量 [英] Unset php session variable
本文介绍了取消设置PHP的会话变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我通过AJAX传递一个id由$ _SESSION被删除。 AJAX的部分工作正常,在开机自检php的接收ID,但vairable无法取消设置。为什么???在这里,我的code:
I'm passing an id by ajax to be removed by $_SESSION. The ajax part works fine, the php receive in POST the id, but the vairable can't be unset. Why??? Here my code:
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接收部分:
The php receiving part:
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!
}
}
任何有帮助的建议,为什么我不能取消设置会话变量?谢谢!
Any helpful suggestion why I can't unset the session variable? Thanks!
推荐答案
我有我自己的解决方案:
I got my own solution:
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";
}
}
这是因为数组是这样的:
That's because the array was like this:
Array
(
[0] => Array
(
[id] => 3
[name] => Collier Ano petit
[price] => 45
[quant] => 1
[ptotal] => 45
)
)
这篇关于取消设置PHP的会话变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文