单击“删除按钮”时,我无法从购物车中删除该项目?

时间:2013-04-22 13:19:09

标签: php session shopping-cart shopping

我有购物车页面,但问题是我点击remove button时无法从购物车中删除该商品?

1)我想通过产品pid删除购物车中的商品?
2)当我回显$pid=intval($pid);它显示PID?在购物车页面上?

产品页面

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

<form method="post">
 <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'];   

if($_REQUEST['command']=='delete' && $pid>0){
  $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']);
}


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

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

购物车页面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();
}
}

1 个答案:

答案 0 :(得分:0)

请修改您的表格: -

  <form  name="form1" method="post">     
  <input type="hidden" name="pid" value="<?php echo $pid?>" />
  <input type="hidden" name="command" value="delete" /> 
  <input type="button" class="button2" value="Remove" onclick="javascript:del(<?php echo $pid?>)" />
  </form>