通过ajax和jquery用php删除MySQL数据,显示没有删除数据的页面
我有这个链接(如下所示)。当它被点击时,它会将帖子的 id 发送到名为 delete_post.php 的 PHP 文件,该文件又会从我的 MySQL 数据库中删除具有该 id 的帖子。这非常有效,只是在帖子被删除后,帖子仍然显示在页面上,直到刷新为止。我怎样才能做到这一点,以便在我单击链接后立即将帖子从我的页面中删除,而不是在单击链接后和刷新页面后。
<a href="javascript:$.post('delete_post.php', { id: '$row[id]' } );" class='delete_post' title='delete post'>delete post</a>
I have this link (shown below). When it is clicked it sends the id of a post to a PHP file called delete_post.php which in turn deletes the post with that id out of my MySQL database. This works great except that after the post is deleted the post is still displayed on the page until it is refreshed. How could I make it so the post is removed from my page as soon as I click the link, rather than after I click the link and after I refresh the page.
<a href="javascript:$.post('delete_post.php', { id: '$row[id]' } );" class='delete_post' title='delete post'>delete post</a>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不熟悉 jQuery 代码(如果您想要示例,我可以在 Prototype 中完成),但是,您应该将侦听器附加到链接,而不是调用 AJAX 内联。这样你就可以使用 jQuery 的 DOM 遍历函数 向上移动到父 div 或表格行,然后只需在 AJAX 调用成功响应后 .remove() 即可。
I'm not familar with the jQuery code (I could do it in Prototype if you want an example) but, you should attach listeners to the links instead of calling the AJAX inline. That way you can use jQuery's DOM traversal functions to move up to the parent div or table row and just simply .remove() it after you get the successful response from your AJAX call.
post() 函数有一个可选的第三个参数,允许您创建成功时执行的匿名函数。
There is an optional third parameter to the post() function that allows you to create an anonymous function that is executed on success.
$row['id']
)添加到保存您的帖子的 div/表格行(容器)。要在 AJAX 完成后隐藏容器,只需通过添加回调函数来修改您的
$.post
,如下所示:$row['id']
) to the div / table row (the container) that is holding your post.To hide the container after your AJAX is complete, simply modify your
$.post
by adding a callback function like this: