views:

157

answers:

1

Hi guys,

I am trying to use jQuery (latest version) & ajax to poll a mysql db every x seconds, post.php does a simple search query on the table and limits to 1 row. (eg SELECT id FROM TABLE LIMIT 1)

I've got some other jQuery UI (using v1.8) code that displays some modal/dialog boxes on the screen, simply put if post.php returns something from the db I need to initialise the dialog to pop up onto the screen. I've done all the popup stuff I am just having issues joining all these bits together - i've added some pseudo code of how i expect this to work. Thanks in advance

var refreshId = setInterval(function(){
    $.ajax({
       type: "POST",
       url: "post.php",
       data:  "",
       success: function(html){
         $("#responsecontainer").html(html);
       }
   });  
}, 2000 );s

/* proposed pseudocode */
if (ajax is successful & returns a db row to #responsecontainer) {
   show jQueryUI modal (done this bit already fortunately)
}
+1  A: 

Why don't you just handle everything within the success block? As you seem to poll for some data, I would also suggest to use JSON.

var refreshId = setInterval(function(){
    $.getJSON('post.php', function(data){
        if (data.message.length > 0) {
            $("#responsecontainer").html(data.message);
            /* your jquery ui stuff here */
        }
    });  
}, 2000);

See jQuery getJSON documentation for more details.

Note: POST should be used to send data, GET to receive data. If you would like to know why and some additional details, you might want to have a look at REST.

eteubert
Thanks mate! That's a great in-depth answer! Nice one thanks again :)
Gav