Jump to content

[SOLVED] Using AJAX to refresh one small part of .php page?


markvaughn2006

Recommended Posts

I'm making a PHP/MySQL game where you can travel to different locations and there is PHP code that tells you who else is in the room with you but the problem is that if someone else enters the room you can not see them unless you refresh the page. Is there any way to use AJAX to constantly refresh only this small part of the page with the PHP code that is telling you who is there with you?? Thanks for any help!!!

You could create a javascript function that you run periodically by using setTimeout

 

Here is a simple example without the ajax part:

<script>
// function for reloading the viewport div (use this function for making an ajax request)
function reloadContent(){
var randomnumber = Math.floor(Math.random()*100);
document.getElementById('viewport').innerHTML = "random number = "+randomnumber;
// execute this function for every second
setTimeout('reloadContent()', 1000);
}
window.onload = function(){
reloadContent();
}
</script>
<div id="viewport">loading</div>

 

You also might also want to look into "Comet ajax"

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.