Hi there Everybody! I am facing a little issue. I have a DIV where I need to show the count of rows from the database. The problem is that when the page refresh the count is not still updated because the database mySQL takes a while, so I have to refresh the page again. Do you know how can I show the count of the rows maybe with javascript? In a way that the count of the rows will be continuosly checked and updated without page reloading.. If I need jQuery for this, just to let you know I am on version 1.3.2 Let me know! Thanks so much!
2 Answers
yea sure, ajax it... first, it does sound odd that when u refresh the page the database isnt ready, but lets assume there is a sync issue where db gets updated out of sync with the current page... the solution is ajax
function doCountUpdate(){
$.get("url_to_return_count",function(data){
assuming the returned data is a number
$("#myDiv").text(data);
setTimeout(doCountUpdate, 1000);
}
}
4 Comments
Piotr Kula
is recalling ajax every 1 second a good idea? maybe more like 10-30 seconds.even a minute..
Ayyash
just saying... but yeah ive done 1 second gaps before, the page is so small a png image call is probably heavier... anyways, a good performance test at the end is always a good idea
DiegoP.
What the other file should do on the other end? just an "echo" function? or what? I am new to ajax. Thanks
jimy
@DiegoP just a echo will do but sending in json_encode format is better option
I think at page load, You can do that by simply a ajax call to a function and return the row and display it on a DIV.
$(document).ready(function() {
// put all your jQuery goodness in here.
$.ajax({
url: "somepage.php",
success: function(data){
$('#div_id').text(data);
}
});
});
3 Comments
DiegoP.
The problem is that the count it's not on an external page. It's a variable inside the same page, so I can't use an ajax call like that in this situation. Any other good advise? thanks so much!
Saic Siquot
yes, of course you can. you must also write "somepage.php" as @ime example shows. it only returns rows count. so the called page replaces inital value
Imran Khan
@DiegoP, you can the self page also... by ajax, load, etc..... OR you can assign that php variable to js varaiable, and then display it on div..e.g: => var count=<?=$count?>