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
https://forums.phpfreaks.com/topic/125373-solved-refresh-div/
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
https://forums.phpfreaks.com/topic/125373-solved-refresh-div/#findComment-648298
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
https://forums.phpfreaks.com/topic/125373-solved-refresh-div/#findComment-648307
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
https://forums.phpfreaks.com/topic/125373-solved-refresh-div/#findComment-648312
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
https://forums.phpfreaks.com/topic/125373-solved-refresh-div/#findComment-648321
Share on other sites

GOOD CALL... firebug reports allowed me to fix it the javascript to the point it is now actually requesting pieces.php every 6 (now set to 10) but I get this error from firebug.

Firebug needs to POST to the server to get this information for url:

http://www.************/pieces.php

Link to comment
https://forums.phpfreaks.com/topic/125373-solved-refresh-div/#findComment-648343
Share on other sites

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.