无法从购物车中删除商品

时间:2013-10-10 06:25:12

标签: php arrays session

我又这么问了 这更具体。

     $cartOutput.='<form method="post" action="cart.php">
     <input type="submit"name="deletebtn'.$item_id.'" value="remove"/>
     <input type="hidden" name="index_to_remove" value="'.$i.'"</form>';

在这个index_to_remove中是通过隐藏的输入类型来形式...我创建了一个带有删除按钮的表单,并通过隐藏的输出字段我传递了我想要从购物车中删除的项目的索引并实现了这个code.But它不工作.......

      <?php
       /////////////////////////////////////////////////////////
        // if user wants to remove an item from cart
         ////////////////////////////////////////////////////////
          if(isset($_POST['index_to_remove']) && $_POST['index_to_remove']=!"")
         {  
         //access the array and rum code to remove that array index
             $key_to_remove=$_POST['index_to_remove'];
          if(count($_SESSION['cart_array'])<=1)
          {
               unset($_SESSION['cart_array']);
               sort($_SESSION['cart_array']);
           }
   else
      {
               unset($_SESSION["cart_array"][$key_to_remove]);
               sort($_SESSION['cart_array']);
               echo count($_SESSION['cart_array']);
     }
 }

    ?>

3 个答案:

答案 0 :(得分:0)

您的HTML已损坏

更改

<input type="hidden" name="index_to_remove" value="'.$i.'"</form>';

<input type="hidden" name="index_to_remove" value='$i'></form>';

答案 1 :(得分:0)

替换此行

if(isset($_POST['index_to_remove']) && $_POST['index_to_remove']=!"")

if(isset($_POST['index_to_remove']) && $_POST['index_to_remove']!="")

因为它将index_to_remove值更改为“1”而不是post。中的值。

你的html标签也没有正确关闭。

答案 2 :(得分:0)

问题在于:

if(isset($_POST['index_to_remove']) && $_POST['index_to_remove']=!"")

在第二部分你有=!“”

这计算为$ var equals not not“并因此返回true(并将$ _POST ['index_to_remove']设置为true,然后在if中使用)。我相信你正在寻找!=或!==这意味着不等于。