Jump to content

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

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.