简体   繁体   English

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

[英]How do I unset session products in shopping cart using remove button?

How do I unset session products in a shopping cart using a remove button? 如何使用“删除”按钮取消购物车中的会话产品? I have the below code; 我有以下代码; 'clear cart' is working and unsets the session for all items from the shopping. “清除购物车”正在运行,并取消了购物中所有商品的会话。 But the problem is that 'remove' doesn't unset single products through pid? 但是问题在于,“删除”是否不会通过pid取消设置单个产品?

Php : Php

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

is no $_REQUEST['product1'] in your form, maybe 您的表单中没有$ _REQUEST ['product1'],也许

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

$_REQUEST['remove'] also $ _REQUEST ['remove']也

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

I don't see any $_REQUEST['remove'] to be sent. 我看不到要发送的任何$ _REQUEST ['remove']。

Your clear command also don't send $pid, isn't it necessary? 您的清除命令也不发送$ pid,不是必须的吗?

In fact, clear() sends query string pid=&command=clear 实际上,clear()发送查询字符串pid=&command=clear

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM