PDA

View Full Version : refresh part of a page.


brad7928
05-31-2009, 05:26 AM
Hi everyone,

i have this webpage that is storing messages posted into a database. what i'm trying to do is:

check how many messages are in the database on load, then have a loop that every 10 seconds will check if there are more messages, if so display them, if not start the loop again.

i was looking to do this with php and use mysql_num_rows($query) to check the rows but couldn't figure out how to get the loop.

so i have been directed to use AJAX to do it.

any idea's on where to start? i have read the tutorial on ajax at w3school.com, but that's about as far as my AJAX knowledge goes...:D

godofreality
06-04-2009, 08:27 AM
this should do it


$result = mysql_query("your query data here");
$rows = mysql_num_rows($result);
echo $rows;



setInterval ("check()",10000);
function check()
{
var trick = Math.floor(Math.random()*100000);
var page = "*****insert into this spot the name of the php file that has the query on it*****";
var XMLHttp = false;
if (window.XMLHttpRequest)
{
XMLHttp = new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
XMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
if (XMLHttp)
{
XMLHttp.open("POST", page+"?"+trick);
XMLHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
XMLHttp.onreadystatechange = function()
{
if (XMLHttp.readyState==4)
{
var amount = document.getElementById('message_quantity').innerHTML;
if(XMLHttp.responseText > amount)
{
document.getElementById('message_quantity').innerHTML = if(XMLHttp.responseText;
}
}
}
XMLHttp.send(null);
}
}



/* place
<span id="message_quantity"></span>
onto your html page where you would like it to display the amount of messages
and your <body> tag should be <body onload="check();">
*/