2

我有以下代码可以删除一条 mysql 记录,我想对其进行转换,因此它将是一个 ajax/jquery 代码,以便在从表中删除记录后我可以留在同一页面。现在,它工作正常,但不在同一页面上,我需要刷新页面才能看到结果。这只是完整代码的一部分。所有代码都在一页上。

//get the mysql results
//this is a repeated region
<tr>
       <td><?php echo $row['id']; ?></td>
       <td><?php echo $row['title']; ?></td>
       <td><?php echo $row['description']; ?></td>
       <td><a href="manage.php?id=<?php echo $row['id']; ?>">Delete</a>
       </td>
</tr>


if ((isset($_GET['deleteid'])) && ($_GET['deleteid'] != "") &&             (isset($_POST['delete']))) {
  $deleteSQL = sprintf("DELETE FROM my_table WHERE id=%s",
                       GetSQLValueString($_GET['deleteid'], "int"));
4

2 回答 2

10
<tr>
       <td><?php echo $row['id']; ?></td>
       <td><?php echo $row['title']; ?></td>
       <td><?php echo $row['description']; ?></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:'delete_page.php',
      data:'delete_id='+del_id,
      success:function(data) {
        if(data) {   // DO SOMETHING
        } else { // DO SOMETHING }
      }
   });
 });
});

现在在“delete_page.php”页面中执行此操作

$id = $_POST['delete_id'];
$query = "delete from TABLE NAME where ID = $id";

休息我希望你知道。希望这可以帮助

于 2013-01-01T10:26:21.107 回答
0

包括 jQuery 和类似的东西就可以了

<tr>
       <td><?php echo $row['id']; ?></td>
       <td><?php echo $row['title']; ?></td>
       <td><?php echo $row['description']; ?></td>
       <td><a href="#" onClick="delete (<?php echo $row['id']; ?>); return false;">Delete</a>
       </td>
</tr>

<script>
function delete(id){
  $.post("manage.php", { deleteid: id , delete: true} );
}
</script>
于 2012-12-31T22:49:31.283 回答