php 如何使用jquery删除mysql记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14106568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:38:58  来源:igfitidea点击:

How to delete a mysql record with jquery

phpjquerymysqlajaxsql-delete

提问by user1932820

I have this following code that delete a mysql record and I would like to transform it so It would be a ajax/jquery code so I can stay in the same page after the record has been deleted from the table. Now, it working fine but it not on the same page and I need to refresh the page to see the result. This is only a part of the full code. All the code in on one page.

我有以下删除 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"));

回答by Roger

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

Now in the section write this

现在在这个部分写这个

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

Now in the 'delete_page.php' page do this

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

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

Rest I hope you know. Hope this helps

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

回答by Jimmy Kane

Include jQuery and something like this will do the trick

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