2017-05-28 68 views
0

我試圖刪除一些記錄,形成我的數據庫沿着它服從,使其出現在表中的規則,多個記錄,所以我試圖使用PHP和AJAX來完成這個動作。雖然我在線複製代碼以執行操作。 但是,這是我注意到,每當我選擇,我要刪除的記錄,它只會淡出選定行,但瀏覽網頁的記錄將會重新出現,這意味着我失去了一些東西,我不知道刷新,這裏是代碼\使用複選框以刪除使用PHP和AJAX

的index.php

<?php 
//include auth.php file on all secure pages 
require("../db.php"); 
session_start(); 
if(!isset($_SESSION["username"])){ 
header("Location: login"); 
exit(); } 
?> 
<?php require_once('header.php')?> 
<?php 
//index.php 
$query = "SELECT * FROM consignment WHERE shipmentstatus='Pending'"; 
$result = mysqli_query($con, $query); 
?> 
<div class="container content"> 


    <?php 
    if(mysqli_num_rows($result) > 0) 
    { 
    ?> 
    <div class="table-responsive"> 
    <table id="myTable" class="table table-striped" > 
     <thead> 
    <tr> 
     <th>Consignment No</th> 
     <th>Origin</th> 
     <th>Destination</th> 
     <th>Pickup Date</th> 
     <th>Status</th> 
     <th >Action</th> 
    </tr> 
    </thead> 
    <?php 
    while($row = mysqli_fetch_array($result)) 
    { 
    ?> 
    <tbody> 
    <tr id="<?php echo $row["consignmentno"]; ?>" > 
     <td><?php echo $row["consignmentno"]; ?></td> 
     <td><?php echo $row["shipmentorigin"]; ?></td> 
     <td><?php echo $row["shipmentdestination"]; ?></td> 
     <td><?php echo $row["shipmentpickupdate"]; ?></td> 
     <td><?php echo $row["shipmentstatus"]; ?></td> 
     <td><input type="checkbox" name="customer_id[]" class="delete_customer" value="<?php echo $row["consignmentno"]; ?>" /></td> 
    </tr> 
    </tbody> 
    <?php 
    } 
    ?> 
    </table> 
    </div> 
    <?php 
    } 
    ?> 
    <div align="center"> 
    <button type="button" name="btn_delete" id="btn_delete" class="btn btn-success">Delete</button> 
    </div> 
</body> 
</html> 

<script> 
$(document).ready(function(){ 

$('#btn_delete').click(function(){ 

    if(confirm("Are you sure you want to delete this?")) 
    { 
    var id = []; 

    $(':checkbox:checked').each(function(i){ 
    id[i] = $(this).val(); 
    }); 

    if(id.length === 0) //tell you if the array is empty 
    { 
    alert("Please Select atleast one checkbox"); 
    } 
    else 
    { 
    $.ajax({ 
    url:'deleterecord.php', 
    method:'POST', 
    data:{id:id}, 
    success:function() 
    { 
     for(var i=0; i<id.length; i++) 
     { 
     $('tr#'+id[i]+'').css('background-color', '#ccc'); 
     $('tr#'+id[i]+'').fadeOut('slow'); 
     } 
    } 

    }); 
    } 

    } 
    else 
    { 
    return false; 
    } 
}); 

}); 
</script> 

<?php require_once('footer.php')?> 

這裏是刪除頁面處理器 deleterecord.php

<?php 
require("../db.php"); 
session_start(); 
if(!isset($_SESSION["username"])){ 
header("Location: login"); 
exit(); } 
?> 
<?php 
if(isset($_POST["id"])) 
{ 
foreach($_POST["id"] as $id) 
{ 
    $query = "DELETE FROM consignment WHERE consignmentno = '".$id."'"; 
    mysqli_query($con, $query); 
} 
} 

?> 
+0

設置錯誤日誌記錄趕上警告,並檢查你得到什麼錯誤。它可能是你的數據庫連接,或者你的查詢。 –

+0

您已開放給SQL注入,參數化該查詢。您還可以使用1個查詢執行所有刪除操作。 – chris85

+0

嘗試檢查數組id中正在發送的值。您可以通過瀏覽器的控制檯執行此操作。 –

回答

0

****這裏是測試全碼****

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
</head> 

<div class="container content"> 
    <table id="myTable" class="table table-striped" border="1"> 
     <thead> 
      <tr> 
       <th>Consignment No</th> 
       <th>Origin</th> 
       <th>Destination</th> 
       <th>Pickup Date</th> 
       <th>Status</th> 
       <th >Action</th> 
      </tr> 
     </thead> 
     <tbody> 
      <tr> 
       <td></td> 
       <td></td> 
       <td></td> 
       <td></td> 
       <td></td> 
       <td><input type="checkbox" name='consignment_no[]' value="1" /></td> 
      </tr> 
      <tr> 
       <td></td> 
       <td></td> 
       <td></td> 
       <td></td> 
       <td></td> 
       <td><input type="checkbox" name='consignment_no[]' value="2" /></td> 
      </tr> 
      <tr> 
       <td></td> 
       <td></td> 
       <td></td> 
       <td></td> 
       <td></td> 
       <td><input type="checkbox" name='consignment_no[]' value="3" /></td> 
      </tr> 
     </tbody> 
     <tr> 
      <td colspan="6" align="center"><button type="button" name="btn_delete" id="btn_delete" class="btn btn-danger">Delete Records</button></td> 
     </tr> 
    </table> 
</div> 
</div> 

<script> 

$(document).ready(function() { 

    $('#btn_delete').click(function() { 

     if (confirm("Are you sure you want to delete this?")) 
     { 
      var id = []; 

      $(':checkbox:checked').each(function (i) { 
       id[i] = $(this).val(); 
      }); 


      if (id.length === 0) //tell you if the array is empty 
      { 
       alert("Please Select atleast one checkbox"); 
      } else 
      { 
       $.ajax({ 
        url: 'deleterecord.php', 
        method: 'POST', 
        data: {id: id}, 
        success: function() 
        { 
         for (var i = 0; i < id.length; i++) 
         { 
         // $('tr#' + id[i] + '').css('background-color', '#ccc'); 
          // $('tr#' + id[i] + '').fadeOut('slow'); 

          $('tr').filter(':has(:checkbox:checked)').find('td').each(function() { 

           $(this).css('background-color', '#ccc'); 
           $(this).fadeOut('slow'); 
           // this = td element 
          }); 
         } 
        } 

       }); 
      } 

     } else 
     { 
      return false; 
     } 
    }); 

}); 
</script> 

</html> 
+2

'$ post'是無效的語法,沒有索引''consignment_no''被髮送。 – charlietfl

+0

你應該使用ajax處理它..你可以發送你的JavaScript/jQuery代碼? –

+0

OP *** ***是用ajax ...看看這個問題 – charlietfl

0

可能存在ID某些問題。 嘗試在腳本中使用這個

$.each($(':checkbox:checked'),function(){ 
id.push($(this).val()); 
});