簡體   English   中英

如何使用“刪除”按鈕取消購物車中的會話產品?

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

如何使用“刪除”按鈕取消購物車中的會話產品? 我有以下代碼; “清除購物車”正在運行,並取消了購物中所有商品的會話。 但是問題在於,“刪除”是否不會通過pid取消設置單個產品?

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

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>
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']也

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

我看不到要發送的任何$ _REQUEST ['remove']。

您的清除命令也不發送$ pid,不是必須的嗎?

實際上,clear()發送查詢字符串pid=&command=clear

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM