在按钮单击事件上取消设置会话变量 [英] Unset session variable on button click event

查看:47
本文介绍了在按钮单击事件上取消设置会话变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用删除按钮"单击正确取消设置会话变量.我有两个页面:产品页面购物车页面,但问题是我的购物车中有一个无法正常工作的删除按钮".

I can't unset a session variable properly using a "remove button" click. I have two pages: product page and shopping cart page, but problem is that I have a "remove button" in the shopping cart which is not working properly.

当我点击删除按钮"时,它应该运行 unset($_SESSION['product1'].

When I click on "remove button" it should run unset($_SESSION['product1'].

但是当我选择另一个项目时,它会显示上一个会话项目",所以这意味着删除按钮没有正确运行 unset($_SESSION['product1'].我该如何解决这个问题?

But when I select another item then it can display "previous session item" so it means remove button does not run unset($_SESSION['product1'] properly. How do I solve this issue?

产品页面

session_start();
$id=$_REQUEST['id'];
$_SESSION['pid1']= $_POST['ids']; //Product ID//
$_SESSION['product1'][]=$_POST['product'];

<form method="post">
<input type="hidden" name="product[]" value="<?php echo $row1['product']; ?>" />
<input type="hidden" name="ids" value="<?php echo $id?>" />
</form>

<input type="submit" class="button1" name="addtocart" value="Add To Cart"
/>

</form>

购物车页面

session_start();
$pid=$_SESSION['pid1'];   

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($_SESSION['product1'][$pid]);
}



<form  name="form1" method="post">  
<input type="hidden" name="pid" />
<input type="hidden" name="command" />  

<input type="button" class="button2" value="Remove" 
onclick="javascript:del(<?php echo $pid?>)"/>

购物车页面Javascript

<script language="javascript">
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();
}
}
</script>

推荐答案

可能是由于

  1. $pid=$_SESSION['pid1']; 而不是 $pid=$_POST['pid1'];

与环绕按钮的标签绑定的事件,您可以使用

event that is tied to tag wrapped around a button, you coud just use

<input type="button" class="button2" value="Remove" onclick="del('<?php echo $pid?>');"/>

这篇关于在按钮单击事件上取消设置会话变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆