0

Okay, so what I'm trying to do is delete a row from my DB. Then update my list in html/php.

function deleteThis(blogid) {
var result="";
 $.ajax({
  url: 'deleter.php',
  type: 'POST',
  data: { id: blogid },
  success:function(data) {

     result = data; 
     alert(result);
          }
});
if (result!=""){

  $("#contentContainer").html(showAll("contentAllBlogs"));
};
};

Using this code (above), I am able to successfully delete data from my database. My problem is reloading the #contentContainer after the DB has been updated.

If I use setTimeout, and wait a few seconds before triggering this code (below)

$("#contentContainer").html(showAll("contentAllBlogs"));

It actually works, my list gets updated. But I don't want to use setTimeout. Is there a way to trigger this code after the DB is finished updating?

Please, help me. Thank you in advance.

RJDO
  • 65
  • 1
  • 1
  • 7
  • 2
    Put the function into the success where it belongs – mplungjan Nov 22 '15 at 15:12
  • 1
    Why you don't put `$("#contentContainer").html(showAll("contentAllBlogs"));` inside the success method? – Alex Nov 22 '15 at 15:13
  • I actually did try that. But it doesn't work. I think this is what happens. The code $("#contentContainer").html(showAll("contentAllBlogs")); gets triggered before the DB is finished updating. – RJDO Nov 22 '15 at 15:17
  • Have a look at the second answer here: http://stackoverflow.com/questions/14754619/jquery-ajax-success-callback-function-definition - if your php returns before updating the DB, then you need to change the php – mplungjan Nov 22 '15 at 15:19
  • Sir, are you implying to use .done() ? i tried it, but it doesn't seem to work. – RJDO Nov 22 '15 at 15:26
  • You can use `complete` setting of `ajax` request for this, [see here](http://pastebin.com/tPKcfvUa). – Rajdeep Paul Nov 22 '15 at 15:37
  • Thank you very much Sir !! That was neat. Now I don't need all the setInterval I wrote. They're ugly. lols haha Thanks again. – RJDO Nov 22 '15 at 15:43
  • You're very welcome! :) – Rajdeep Paul Nov 22 '15 at 15:45

1 Answers1

0

you can not access result data out the ajax function because in if() statement result value is undefined.

function deleteThis(blogid) {
var result="";
 $.ajax({
  url: 'deleter.php',
  type: 'POST',
  data: { id: blogid },
  success:function(data) {

     result = data; 
     displayData();
          }
});

function displayData(){
$("#contentContainer").html(showAll("contentAllBlogs"));
}
Zu007
  • 81
  • 1
  • 2
  • 8