0

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>

3 Answers 3

2
  1. Add an ID (using the $row['id']) to the div / table row (the container) that is holding your post.
  2. After the AJAX is completed, you then simply hide the container.

To hide the container after your AJAX is complete, simply modify your $.post by adding a callback function like this:

javascript:$.post('delete_post.php', { id: '$row[id]' } , function(){ $('#row$row[id]').hide(); });
Sign up to request clarification or add additional context in comments.

1 Comment

If you don't need to access the element again, you should remove it from the dom entirely using .remove() $('#row$row[id]').hide(0,function() {$(this).remove()}
0

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.

Comments

-1

There is an optional third parameter to the post() function that allows you to create an anonymous function that is executed on success.

<a href="javascript:$.post('delete_post.php', { id: '$row[id]' }, function() { location.reload(); } );" class='delete_post' title='delete post'>delete post</a>

2 Comments

The whole reason of doing this with ajax was so that I did not have to reload the whole page. Thanks for your help though :)
Reloading page? This is not the ajax thing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.