Jump to content

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


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"

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.