2015-12-29 29 views
0
echo '<td><a class="delete" href="#" id="'.$row['Id'].'">Delete</a></td>'; 



<script type="text/javascript"> 
     $(function() { 
     $(".delete").click(function(){ 
     var element = $(this); 
     var del_id = element.attr("id");; 
     var info = 'id=' + del_id; 
     if(confirm("Are you sure you want to delete this?")) 
     { 
      $.ajax({ 
      type: "POST", 
      url: "ajax_delete.php", 
      data: info, 
      success: function(){ 
      } 
     }); 
      $(this).parents("#checkboxlist").animate({ backgroundColor: "#003" }, "slow") 
      .animate({ opacity: "hide" }, "slow"); 
      } 
     return false; 
     }); 
     }); 

     </script> 

ajax_delete.php阿賈克斯同時刪除PHP的MySQL行

<?php 
session_start(); 
include "config.php"; 

$id=$_REQUEST['id']; 

    $record = mysql_query("delete from contact_form where Id = '$id'",$con); 
    if($record) 
    { 

     echo '<META HTTP-EQUIV="Refresh" Content="0; URL=list.php">'; 
     $_SESSION['deleted']="yes"; 

    } 

    else 
    { 
     echo mysql_error(); 
    } 


?> 

不工作我也加入了jQuery庫。試過每一件可能的事情。但桌子不刷新。數據被刪除。但只有刷新頁面時纔會反映出來。我對此感到沮喪。

回答

4

您需要從頁面中刪除它,然後在成功刪除它之後。

success: function(){ 
    element.remove(); 
} 
+0

哪裏可以找到'刪除()'爲元素的功能?我無法在jQuery或JS中找到! –

0

試試這個嗎?

<script type="text/javascript"> 
     $(function() { 
     $(".delete").click(function(){ 
     var element = $(this); 
     var del_id = element.attr("id");; 

     if(confirm("Are you sure you want to delete this?")) 
     { 
      $.ajax({ 
      type: "POST", 
      url: "ajax_delete.php", 
      data: {id:del_id}, 
      success: function(){ 
       element.parents("#checkboxlist").animate({ backgroundColor: "#003" }, "slow") 
       .animate({ opacity: "hide" }, "slow"); 
       } 
     }); 
      } 
     }); 

     }); 

     </script> 
0

首先把你的animation codesuccess callback

更新腳本

<script type="text/javascript"> 
    $(function() { 
     $(".delete").click(function() { 
      var element = $(this); 
      var del_id = element.attr("id"); 
      ; 
      var info = 'id=' + del_id; 
      if (confirm("Are you sure you want to delete this?")) 
      { 
       $.ajax({ 
        type: "POST", 
        url: "ajax_delete.php", 
        data: info, 
        success: function() { 
         element.parents("#checkboxlist").animate({backgroundColor: "#003"}, "slow") 
         .animate({opacity: "hide"}, "slow").remove(); //use element and after hide parent remove it 
        } 
       }); 

      } 
      return false; 
     }); 
    }); 

</script> 
0

您可以從表中刪除一行JS(Ajax調用不會自動爲您做到這一點!)。像這樣做:

$.ajax({ 
     type: "POST", 
     url: "ajax_delete.php", 
     data: info, 
     success: function(){ 
      element.closest('tr').remove(); //remove the parent row of the delete button 
     } 
}); 
0

嘗試增加$(this).parents到阿賈克斯成功PARAM

success: function(){ 
     element.closest("#checkboxlist").animate({ backgroundColor: "#003" }, "slow") 
       .animate({ opacity: "hide" }, "slow");   
    }