Jump to content

[SOLVED] Refresh div


Brian W

Recommended Posts

I have a div(i can make it a table or whatever is needed) that contains a repeat region of rows in my database. That works fine, i'm fine it php. I want just that div(or whatever) to reload every 10 seconds so that I get the most recent information. can this be done using ajax or simply js?

Link to comment
Share on other sites

In your JS, you'll need this:

function getHTTPObject() {
var xmlhttp;
if (!xmlhttp && typeof XMLHttpRequest!='undefined'){
	try{
		xmlhttp = new XMLHttpRequest();
	} catch (e){
		xmlhttp = false;
        }
}
return xmlhttp;
}
var http = getHTTPObject();

Plus your JS function that loads the PHP data:

function changeDiv(){
var url = "somefile.php";
http.open("GET", url, false);
http.send(null);
document.getElementById('somediv').innerHTML = http.responseText;
}

 

Then, in your "somefile.php," just echo what you want in your div.

 

If you want to pass on variables, just push them to your JS file, then pass them onto your url variable like you would any other URL string.

 

Finally, add an event that calls the JS function,

Link to comment
Share on other sites

i don't know anything about javascript or ajax... just enough so sorda understand that logic you gave me. where should I look for the function to run the function ever 10 seconds or whatever? am i looking for keywords like "refresh" because I haven't really found anything like that on google yet.

Also, my div content will be a repeat region from a mysql quarry. I have that in a separate .php file like you said, do i need to take any extra steps to get that to work? no strings are passed per se.

 

Thank you

Link to comment
Share on other sites

<script type="text/javascript">function getHTTPObject() {
var xmlhttp;
if (!xmlhttp && typeof XMLHttpRequest!='undefined'){
	try{
		xmlhttp = new XMLHttpRequest();
	} catch (e){
		xmlhttp = false;
        }
}
return xmlhttp;
}
var http = getHTTPObject(); 

function changeDiv(){
var url = "pieces.php";
http.open("GET", url, false);
http.send(null);
document.getElementById('pieces').innerHTML = http.responseText;</script>
<div id="pieces" onLoad="window.setInterval("changeDiv()", 6000);"> </div>

This did not work... please critique

 

thank you

Link to comment
Share on other sites

<script type="text/javascript">function getHTTPObject() {
var xmlhttp;
if (!xmlhttp && typeof XMLHttpRequest!='undefined'){
	try{
		xmlhttp = new XMLHttpRequest();
	} catch (e){
		xmlhttp = false;
        }
}
return xmlhttp;
}
var http = getHTTPObject(); 

function changeDiv(){
var url = "pieces.php";
http.open("GET", url, true);
http.send(null);
document.getElementById('pieces').innerHTML = http.responseText;</script>
</head>

<body  onLoad="window.setInterval("changeDiv()", 6000);">

<div id="pieces"></div>

Tried this, not seeming to work.

Link to comment
Share on other sites

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.