如何从php页面中删除整行

时间:2015-05-23 14:08:27

标签: javascript php jquery ajax

我从数据库中删除了行,但我想从php页面的界面中删除该行。非常感谢。

mypage.php

<tr>
       <td><?php echo $row['id']; ?></td>
       <td><?php echo $row['title']; ?></td>
       <td><div class="delete_class" id="<?php echo $row['id']; ?>">Delete</div></td>
</tr>

$(document).ready(function(){
 $(".delete_class").click(function(){
   var del_id = $(this).attr('id');
   $.ajax({
      type:'POST',
      url:'check.php',
      data:'delt_id='+del_id,
      success:function(data) {
        if(data) {   alert("successfully deleted data");
        } else { // DO SOMETHING }
      }
   });
 });
});

check.php
$id = $_POST['delt_id'];
$query = "delete from TABLE NAME where ID = $id";

2 个答案:

答案 0 :(得分:0)

您需要remove() <tr>元素。

// you currently have this   
var del_id = $(this).attr('id');

// add this, step up to td, step up to tr
var $tableRow = $(this).parent().parent();

// or add this, finds the nearest tr
var $tableRow = $(this).closest('tr');

// then remove it (once you know the request was successful)
$tableRow.remove();

答案 1 :(得分:0)

最简单的解决方案是在成功回调中删除客户端(如果成功只在您确定该行已从数据库中删除服务器端时才触发):

main()