Javascript 如何使用“删除”按钮取消购物车中的会话产品?
如何使用移除按钮取消购物车中的会话产品?我有下面的代码;'“清除购物车”正在工作,并为购物中的所有项目取消设置会话。但问题是“删除”不会通过pid取消单个产品的设置 Php:Javascript 如何使用“删除”按钮取消购物车中的会话产品?,javascript,php,session,shopping-cart,Javascript,Php,Session,Shopping Cart,如何使用移除按钮取消购物车中的会话产品?我有下面的代码;'“清除购物车”正在工作,并为购物中的所有项目取消设置会话。但问题是“删除”不会通过pid取消单个产品的设置 Php: session_start(); $pid=$_SESSION['pid']; function remove_product($pid){ $pid=intval($pid); $max=count($_SESSION['product1']); for($i=0;$i<$max;$i++){
session_start();
$pid=$_SESSION['pid'];
function remove_product($pid){
$pid=intval($pid);
$max=count($_SESSION['product1']);
for($i=0;$i<$max;$i++){
if($pid==$_SESSION['product1'][$i]['pid']){
unset($_SESSION['product1'][$i]);
break;
}
}
$_SESSION['product1']=array_values($_SESSION['product1']);
}
if($_REQUEST['command']=='delete' && $_REQUEST['pid']>0){
remove_product($_REQUEST['product1'][$pid]);
}
if($_REQUEST['command']=='clear' && isset($_REQUEST['remove'])){
unset($_SESSION['image12']);
unset($_SESSION['product1']);
unset($_SESSION['price12']);
unset($_SESSION['itemcode3']);
unset($_SESSION['sizes12']);
}
function del(pid){
if(confirm('Do you really mean to delete this item')){
document.form1.pid.value=pid;
document.form1.command.value='delete';
document.form1.submit();
}
}
function clear_cart(){
if(confirm('This will empty your shopping cart, continue?')){
document.form1.command.value='clear';
document.form1.submit();
}
}
Html:
<form name="form1" method="post">
<input type="hidden" name="pid" />
<input type="hidden" name="command" />
<input type="button" class="button2" value="Clear Cart" onclick="clear_cart()" />
<a href="javascript:del(<?php echo $pid?>)">
<input type="button" class="button2" value="Remove" />
</a>
</form>
您的表单中没有$\u请求['product1'],
也许吧
$\u请求['remove']也可以
if($_REQUEST['command']=='clear' && isset($_REQUEST['remove'])){
unset($_SESSION['image12']);
unset($_SESSION['product1']);
unset($_SESSION['price12']);
unset($_SESSION['itemcode3']);
unset($_SESSION['sizes12']);
}
我没有看到任何要发送的$u请求['remove']
您的clear命令也不会发送$pid,这不是必需的吗
事实上,clear()发送查询字符串
pid=&command=clear
joe lee然后它应该是$\u SESSION['product1']
,而不是$\u REQUEST['product1']
那么现在的解决方案是什么呢?你对$\u REQUEST['remove']的意思是什么?如果你试图检查是否同时单击了清除或删除按钮,那么检查似乎不正确?在unset()启动之前,您希望检查什么?i、 e.是否发送了pid
,并单击了clear
?
if($_REQUEST['command']=='delete' && $_REQUEST['pid']>0){
remove_product($_REQUEST['pid']);
}
if($_REQUEST['command']=='clear' && isset($_REQUEST['remove'])){
unset($_SESSION['image12']);
unset($_SESSION['product1']);
unset($_SESSION['price12']);
unset($_SESSION['itemcode3']);
unset($_SESSION['sizes12']);
}