How to update all users page automatically after delete of posts on forum page without page reload
Kindly assist please.
I'm working on a forum page with php and jquery/ajax. When i delete a post, my page is immediately updated. However, other logged-in users still see the deleted post until their pages are reloaded. What i want is a functionality that updates all logged in users pages without page reload. My fetch_posts.php file outputs the result of the sql select statement as a JSON encoded data to my jquery file. Here is some of the relevant contents of my jquery file.
- function fetchPosts(){ //To fetch posts from database
- $.getJSON("fetch_posts.php", function(data){
- $.each(data, function(i, value){
- //if post does'nt exist on page b4, prepend posts to doc
- if ($('.media-wrapper .media[data-id = '+value.id+']').length == 0){
- var post = //"some posts'';
- $(".media-wrapper").prepend(post);
- }
- });
- });
- }
- setInterval(fetchPosts, 5000);
- //To delete a post the logged in user authored
- $(document).on("click", ".post_delete", function(e){
- e.preventDefault();
- var post_id = $(this).attr('del-id');
- var post= $('.media-wrapper').find('.media[data-id = '+post_id+']');
- if(confirm("Are you sure you want to delete the post?")){
- $.post("del_post.php", {"post_id": post_id}, function(data){
- if (data == "yes"){
- $(post).remove();
- fetchPosts();
- }
- });
- }
- });
Thanks in anticipation of your guidance in resolving this.