2016-04-01 15 views
0

ich mit meinem Warenkorb ziemlich fertig bin. Ich kann den Einkaufswagen aktualisieren, aber wenn ich auf die Schaltfläche zum Löschen neben dem Objekt klicke, wird es auf eine leere Seite umgeleitet. Dies ist die Spitze meiner cart.php ist:Schaltfläche Löschen in den Warenkorb funktioniert nicht

<?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; 
    } 
    } 
    ?> 

Das ist mein Wagen Form:

<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> 

Schließlich ist dies die jquery ein Element zu löschen:

$(".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?"; 

    }); 

Und

 <?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'; 
    } 
    ?> 
: die delete.php Seite der Artikel aus der Warenkorb Sitzung entfernen

Ich bin neu auf E-Commerce-Websites. Ich würde wirklich jede Hilfe schätzen. Dank

+0

Ist das 'js' Feuer sein sollte? Sieht nicht so aus, als wäre 'ebook_id' in JS definiert. Dies steht auch SQL-Injektionen offen. Verwenden Sie vorbereitete Anweisungen und separate Benutzerdaten aus SQL. – chris85

Antwort

0

Auf Ihrer löschen Seite

<?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; 
     } 
    } 

Ihr JS-Code

  $(".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();        
         }); 
       }); 

Ihre Form

<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> 

+0

Danke für die Antwort, aber immer noch das Gleiche. Wenn ich auf die Schaltfläche "Löschen" klicke, wird ein neues Element hinzugefügt, anstatt es aus dem Einkaufswagen zu löschen. Ich weiß, dass mein Code ein Durcheinander ist. Das tut mir leid. – raul

Verwandte Themen