Reputation: 2338
I'm trying to figure out a way to have jQuery update a div the moment a table in mysql is updated. I've spent a vigorous amount of hours searching online for an answer, and so far, nothing. Can anyone help me out with this problem?
Upvotes: 2
Views: 2411
Reputation: 14219
Well the steps you should probably take are:
Have a piece of AJAX code that queries the server for a change (like row count changing or something along those lines). Using jQuery you can do that:
function checkUpdates()
{
$.ajax({
type: "POST",
url: 'hasDataChanged.php', // a webservice or other URL that queries the database
data: {},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
// return a JSON string like { "hasChanged" : "true" } or something
if (data.hasChanged) {
// data has changed, do something
}
}
});
}
Then you can use the Javascript method setInterval
to call the code every few seconds. It is unrealistic to do it instantly.
$(document).ready(function() {
setInterval("checkUpdates()", 3000); // Calls the function every 3 seconds
});
Upvotes: 3
Reputation: 10664
Useful links on polling. It attempts to make server send data to browser:
Upvotes: 0
Reputation: 30135
You'll have to poll the database via ajax and php every (couple of) second(s) and check if the data has changed. if so, update the div.
i don't think there's a way of detecting the exact moment the db is updated.
Upvotes: 2