簡體   English   中英

購物車中的“刪除”按鈕不起作用

[英]Delete button in shopping cart is not working

我的購物車已經差不多完成了。 我可以更新購物車,但是當我單擊商品旁邊的刪除按鈕時,它會重定向到空白頁。 這是我的cart.php的頂部:

    <?php
     session_start();
      include ("db_connection.php");//include database connection
       if (isset($_SESSION['cart'])) {
        if(isset($_GET['ebook_id']) && !isset($_POST['update'])){
          $statement=$_SESSION['cart'];
          $find=false;
          $number=0;
      for($i=0;$i<count($statement);$i++){
        if($statement[$i]['ebook_id']==$_GET['ebook_id']){
          $find=true;
          $number=$i;
        }
      }
      if($find==true){
        $statement[$number]['quantity']=$statement[$number]['quantity']+1;
        $_SESSION['cart']=$statement;
      }else{
        $ebook_title="";
        $price=0;
        $ebook_image="";
        $query=mysqli_query($con,"SELECT * FROM ebooks WHERE ebook_id=".$_GET['ebook_id']);
        while ($row=mysqli_fetch_array($query,MYSQLI_ASSOC)) {
          $ebook_title=$row['ebook_title'];
          $price=$row['price'];
          $ebook_image=$row['ebook_image'];
        }
        $newData=array('ebook_id'=>$_GET['ebook_id'],
                'ebook_title'=>$ebook_title,
                'price'=>$price,
                'ebook_image'=>$ebook_image,
                'quantity'=>1);

        array_push($statement, $newData);
        $_SESSION['cart']=$statement;

        }
       }  
     }else{
      if(isset($_GET['ebook_id'])){
       $ebook_title="";
       $ebook_image="";
       $price=0;
       $query=mysqli_query($con,"SELECT * FROM ebooks WHERE ebook_id=".$_GET['ebook_id']);
      while ($row=mysqli_fetch_array($query,MYSQLI_ASSOC)) {
       extract ($row);
       $ebook_title=$row['ebook_title'];
       $ebook_image=$row['ebook_image'];
       $price=$row['price'];
      }
      $statement[]= array('ebook_id' => $_GET['ebook_id'],'ebook_title' => $ebook_title,'ebook_image' => $ebook_image,'price' => $price,'quantity' => 1);

     $_SESSION['cart']=$statement;
     }
    }
    ?>

這是我的購物車表格:

    <form method="POST">
                            <table class="table">
                                <thead>
                                    <tr>
                                        <th colspan="2">E-book</th>
                                        <th>Price</th>
                                        <th>Quantity</th>
                                        <th>Subtotal</th>
                                        <th>Remove</th>
                                    </tr>
                                </thead>
                                <tbody>

                    <tr> 
                      <?php
                       if (isset($_POST['update'])){
                        $arrquantity=$_POST['quantity'];
                        //check validate quantity
                        $valid = 1;
                        for ($i=0; $i < count($arrquantity); $i++) { 
                                if (!is_numeric($arrquantity[$i]) || $arrquantity[$i] < 1) {
                                  $valid=0;
                                  break;
                                }

                        }
                        if ($valid == 1){
                        $cart = unserialize (serialize($_SESSION['cart']));
                        for ($i=0;$i<count($cart);$i++) {
                               $cart[$i]['quantity']=$arrquantity[$i];
                        }
                      }else{
                        $error= "Quantity is invalid";
                      }
                        $_SESSION['cart']=$cart;
                       }
                       //delete ebook in cart


                        $total=0;
                        if (isset($_SESSION['cart'])) {
                          $data=$_SESSION['cart'];
                          $total=0;
                          for ($i=0;$i<count($data);$i++) { 
                            ?>
                            <?php echo isset($error) ? $error :'';?>
                            <div class="ebook">
                             <td><a href=""><img src="<?php echo $data[$i]['ebook_image'];?>"></a></td>
                             <td><?php echo $data[$i]['ebook_title'];?></td>
                             <td>£<?php echo $data[$i]['price'];?></td>
                             <td><input type="text" value="<?php echo $data[$i]['quantity'];?>" data-price="<?php echo $data[$i]['price'];?>" data-id="<?php echo $data[$i]['ebook_id'];?>" class="quantity" name="quantity"></td>  
                             <td class="subtotal">£<?php echo $data[$i]['price']*$data[$i]['quantity'];?></td>
                             <td><a href="cart.php?ebook_id=<?php echo $data[$i]['ebook_id'];?>&action =delete" onclick="return confirm('Are you sure?')" class="delete" data-id="<?php echo $data[$i]['ebook_id'];?>"><i class="fa fa-trash-o"></i></a>
                           </div>
                    </tr> 

                        <?php  
                         $total=($data[$i]['price']*$data[$i]['quantity'])+$total;      
                          }
                        }else{
                          echo "<p class='text-muted'>Your shopping cart is empty</p>";
                        }
                      ?>
                       </tbody>
                                <tfoot>
                                    <tr>
                                        <th colspan="5">Total Price</th>
                                        <th colspan="2" id="total">£<?php echo $total; ?></th>
                                    </tr>
                                </tfoot>
                            </table>

                        </div>
                        <!-- /.table-responsive -->

                        <div class="box-footer">
                            <div class="pull-left">
                                <a href="category.php" class="btn btn-default"><i class="fa fa-chevron-left"></i>Continue shopping</a>
                            </div>
                            <div class="pull-right">
                                <button type="submit" class="btn btn-default" name="update"><i class="fa fa-refresh"></i>Update</button>
                                <button type="submit" class="btn btn-primary">Checkout<i class="fa fa-chevron-right"></i>
                                </button>
                            </div>
                        </div>

                    </form>

最后,這是刪除項目的jQuery:

    $(".delete").click(function(e){
    e.preventDefault();
    var id=$(this).attr('data-id');
    $(this).parentsUntil('.ebook').remove();
    $.post('./js/delete.php',{
        Id:ebook_id
    },function(a){
        location.href="cart.php?";

    });   

和delete.php頁面,從購物車會話中刪除該項目:

       <?php
       session_start();
       $statement=$_SESSION['cart'];
         for($i=0;$i<count($statement);$i++){
          if($statement[$i]['ebook_id']!=$_POST['ebook_id']){
           $newData[]=array(
            'ebook_id'=>$statement[$i]['ebook_id'],
            'ebook_title'=>$statement[$i]['ebook_title'],
            'price'=>$statement[$i]['price'],
            'ebook_image'=>$statement[$i]['ebook_image'],
            'quantity'=>$statement[$i]['quantity']
            );
       }
      }
     if(isset($newData)){
       $_SESSION['cart']=$newData;
    }else{
      unset($_SESSION['cart']);
      echo '0';
    }
    ?>  

我是電子商務網站的新手。 我真的很感謝您的幫助。 謝謝

在您的刪除頁面上

 <?php

   if(isset($_POST['ebook_id'])){
     $query=mysqli_query($con,"DELETE FROM ebooks WHERE ebook_id=".$_POST['ebook_id']);
     if($query==true) {
         return true; 
       }else {
         return false;
         }
    }

您的JS代碼應為

            $(".delete").click(function(e){
              e.preventDefault();
              var id=$(this).attr('data-id');
               $.ajax({
                      type:"POST",    
                      url: "delete.php",
                      data: {ebook_id: id},
                      }).done(function(data) {                             
                         $("#row_"+id+"").hide();                            
                      });
              });   

您的表格

<form method="POST">
 <table class="table">
    <thead>
    <tr>
        <th colspan="2">E-book</th>
        <th>Price</th>
        <th>Quantity</th>
        <th>Subtotal</th>
        <th>Remove</th>
    </tr>
    </thead>
    <tbody>

        <?php
        if (isset($_POST['update'])){
            $arrquantity=$_POST['quantity'];
            //check validate quantity
            $valid = 1;
            for ($i=0; $i < count($arrquantity); $i++) {
                if (!is_numeric($arrquantity[$i]) || $arrquantity[$i] < 1) {
                    $valid=0;
                    break;
                }

            }
            if ($valid == 1){
                $cart = unserialize (serialize($_SESSION['cart']));
                for ($i=0;$i<count($cart);$i++) {
                    $cart[$i]['quantity']=$arrquantity[$i];
                }
            }else{
                $error= "Quantity is invalid";
            }
            $_SESSION['cart']=$cart;
        }
        //delete ebook in cart


        $total=0;
        if (isset($_SESSION['cart'])) {
        $data=$_SESSION['cart'];
        $total=0;
        for ($i=0;$i<count($data);$i++) {
        ?>
        <?php echo isset($error) ? $error :'';?>
         //i don't know why you had open div here, so i replaced the div with tr and tr should have been inside the loop
        <tr class="ebook" id="row_<?php echo $data[$i]['ebook_id'];?>">
            <td><a href=""><img src="<?php echo $data[$i]['ebook_image'];?>"></a></td>
            <td><?php echo $data[$i]['ebook_title'];?></td>
            <td>£<?php echo $data[$i]['price'];?></td>
            <td><input type="text" value="<?php echo $data[$i]['quantity'];?>" data-price="<?php echo $data[$i]['price'];?>" data-id="<?php echo $data[$i]['ebook_id'];?>" class="quantity" name="quantity"></td>
            <td class="subtotal">£<?php echo $data[$i]['price']*$data[$i]['quantity'];?></td>
            <td><a href="cart.php?ebook_id=<?php echo $data[$i]['ebook_id'];?>&action =delete" onclick="return confirm('Are you sure?')" class="delete" data-id="<?php echo $data[$i]['ebook_id'];?>"><i class="fa fa-trash-o"></i></a>
        </tr>
    </tr>

    <?php
    $total=($data[$i]['price']*$data[$i]['quantity'])+$total;
    }
    }else{
        echo "<p class='text-muted'>Your shopping cart is empty</p>";
    }
    ?>
    </tbody>
    <tfoot>
    <tr>
        <th colspan="5">Total Price</th>
        <th colspan="2" id="total">£<?php echo $total; ?></th>
    </tr>
    </tfoot>
</table>

</div>
<!-- /.table-responsive -->

<div class="box-footer">
    <div class="pull-left">
        <a href="category.php" class="btn btn-default"><i class="fa fa-chevron-left"></i>Continue shopping</a>
    </div>
    <div class="pull-right">
        <button type="submit" class="btn btn-default" name="update"><i class="fa fa-refresh"></i>Update</button>
        <button type="submit" class="btn btn-primary">Checkout<i class="fa fa-chevron-right"></i>
        </button>
    </div>
</div>

暫無
暫無

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

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