如何使用删除按钮取消购物车中的会话产品?

时间:2013-04-16 07:26:52

标签: javascript php session shopping-cart

如何使用移除按钮取消购物车中的会话产品?我有以下代码; 'clear cart'正在运行,取消了购物中所有商品的会话。但问题是'删除'不会通过pid取消单个产品?

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']);
}   

的js

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>

2 个答案:

答案 0 :(得分:0)

if($_REQUEST['command']=='delete' && $_REQUEST['pid']>0){
remove_product($_REQUEST['product1'][$pid]);
}

表单中没有$ _REQUEST ['product1'], 也许

if($_REQUEST['command']=='delete' && $_REQUEST['pid']>0){
remove_product($_REQUEST['pid']);
}

$ _ REQUEST ['remove']也

答案 1 :(得分:0)

if($_REQUEST['command']=='clear' && isset($_REQUEST['remove'])){
unset($_SESSION['image12']);
unset($_SESSION['product1']);
unset($_SESSION['price12']);
unset($_SESSION['itemcode3']);
unset($_SESSION['sizes12']);
}   

我没有看到任何$ _REQUEST ['remove']被发送。

你的clear命令也不发送$ pid,是不是有必要?

实际上,clear()发送查询字符串pid=&command=clear