PHP + Ajax auto refresh DIV
File xmlhttp.js
var xmlhttp = false ;
if (!xmlhttp && typeof XMLHttpRequest != ‘undefined’)
{
try {
xmlhttp = new XMLHttpRequest ();
}
catch (e) {
xmlhttp = false}
}
function myXMLHttpRequest ()
{
var xmlhttplocal;
try {
xmlhttplocal = new ActiveXObject (“Msxml2.XMLHTTP”)}
catch (e) {
try {
xmlhttplocal = new ActiveXObject (“Microsoft.XMLHTTP”)}
catch (E) {
xmlhttplocal = false;
}
}
if (!xmlhttplocal && typeof XMLHttpRequest != ‘undefined’) {
try {
var xmlhttplocal = new XMLHttpRequest ();
}
catch (e) {
var xmlhttplocal = false;
}
}
return (xmlhttplocal);
}
var mnmxmlhttp = Array ();
var mnmString = Array ();
var mnmPrevColor = Array ();
var responsestring = Array ();
var myxmlhttp = Array ();
var responseString = new String;
var i=0;
var ii = 0;
function ajax_update()
{
url = “ajax_php.php”;
target2 = document.getElementById (‘content’);
ii = i++;
var content = “i=” + ii ;
mnmxmlhttp = new myXMLHttpRequest ();
if (mnmxmlhttp) {
mnmxmlhttp.open (“POST”, url, true);
mnmxmlhttp.setRequestHeader (‘Content-Type’,
‘application/x-www-form-urlencoded’);
mnmxmlhttp.send (content);
errormatch = new RegExp (“^ERROR:”);
target2 = document.getElementById (‘content’);
mnmxmlhttp.onreadystatechange = function () {
if (mnmxmlhttp.readyState == 4) {
mnmString = mnmxmlhttp.responseText;
if (mnmString.match (errormatch)) {
mnmString = mnmString.substring (6, mnmString.length);
target = document.getElementById (‘content’);
target2.innerHTML = mnmString;
} else {
target = document.getElementById (‘content’);
target2.innerHTML = mnmString;
}
}
}
}
setTimeout(‘ajax_update()’, 20000);
}
File ajax_php.php
<?PHP
echo “<PRE>”;
print_r($_POST);
echo time();
?>
File test.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>PHP + Ajax auto refresh DIV</title> <script language="JavaScript" type="text/javascript" src="xmlhttp.js"></script> </head> <body onload="ajax_update();"> <divid="content">Loading....</div>
</body>
</html> I tested these three scripts more than 10 times, it's works. please just replace the ' “ ' with ' " ' Or download the script here.ajax auto refresh script download
this is all i get:
Array
(
[i] => 1
)
1217961577
where the numbers increase
can you show me your source code?
I modified the code a little bit. I need to have different divs pulling out data from a database and then auto-refresh. I created another div and played around with your code, but I couldn’t get it to work with two different divs.
Any suggestions?
[code]
PHP + Ajax auto refresh DIV
var xmlhttp = false ;
if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
{
try {
xmlhttp = new XMLHttpRequest ();
}
catch (e) {
xmlhttp = false}
}
function myXMLHttpRequest ()
{
var xmlhttplocal;
try {
xmlhttplocal = new ActiveXObject ("Msxml2.XMLHTTP")}
catch (e) {
try {
xmlhttplocal = new ActiveXObject ("Microsoft.XMLHTTP")}
catch (E) {
xmlhttplocal = false;
}
}
if (!xmlhttplocal && typeof XMLHttpRequest != 'undefined') {
try {
var xmlhttplocal = new XMLHttpRequest ();
}
catch (e) {
var xmlhttplocal = false;
}
}
return (xmlhttplocal);
}
var mnmxmlhttp = Array ();
var mnmString = Array ();
var mnmPrevColor = Array ();
var responsestring = Array ();
var myxmlhttp = Array ();
var responseString = new String;
var i=0;
var ii = 0;
function ajax_update()
{
url = "ajax_php.php";
target2 = document.getElementById ('content');
ii = i++;
var content = "i=" + ii ;
mnmxmlhttp = new myXMLHttpRequest ();
if (mnmxmlhttp) {
mnmxmlhttp.open ("POST", url, true);
mnmxmlhttp.setRequestHeader ('Content-Type',
'application/x-www-form-urlencoded');
mnmxmlhttp.send (content);
errormatch = new RegExp ("^ERROR:");
target2 = document.getElementById ('content');
mnmxmlhttp.onreadystatechange = function () {
if (mnmxmlhttp.readyState == 4) {
mnmString = mnmxmlhttp.responseText;
if (mnmString.match (errormatch)) {
mnmString = mnmString.substring (6, mnmString.length);
target = document.getElementById ('content');
target2.innerHTML = mnmString;
} else {
target = document.getElementById ('content');
target2.innerHTML = mnmString;
}
}
}
}
setTimeout('ajax_update()', 2000);
}
function ajax_update2()
{
url = "ajax_php.php";
target2 = document.getElementById ('content2');
ii = i + 5;
var content = "i=" + ii ;
mnmxmlhttp = new myXMLHttpRequest ();
if (mnmxmlhttp) {
mnmxmlhttp.open ("POST", url, true);
mnmxmlhttp.setRequestHeader ('Content-Type',
'application/x-www-form-urlencoded');
mnmxmlhttp.send (content);
errormatch = new RegExp ("^ERROR:");
target2 = document.getElementById ('content2');
mnmxmlhttp.onreadystatechange = function () {
if (mnmxmlhttp.readyState == 4) {
mnmString = mnmxmlhttp.responseText;
if (mnmString.match (errormatch)) {
mnmString = mnmString.substring (6, mnmString.length);
target = document.getElementById ('content2');
target2.innerHTML = mnmString;
} else {
target = document.getElementById ('content2');
target2.innerHTML = mnmString;
}
}
}
}
setTimeout('ajax_update2()', 1000);
}
Loading...
Loading...
[/code]
Never mind! I figured out!!
Thanks for the code, it’s exactly what I needed!
Sorry but this won’t work, it just says Loading and doesn’t show the time or even show the php script (which I checked, it is working). I did replace the “.
If anyone has the working script please send it me! Thanks!
can you download the zip files and test again?
In the download file some of the characters in the code changes, but even when i change them it still does not work.
Hi, i would really appreciate your help, it is not working. Could you write to my email and send those files. In the zip files are messed up. Thanks
hi;
i needed the same script ( the one with the multiple DIV’s) can some one send me the zip files./
thanks
qjvjsfcouyzyfrsawell, hi admin adn people nice forum indeed. how’s life? hope it’s introduce branch
Your download files messed up.
Very nice. Now if there were only a way to be able to have multiple displays with the same refresh for each.
hi! i got same error as Eric had earlier –
Array
(
[i] => 19
)
1233969574
please help me to out of it….
great little script here! but i am also in need of figuring out how to get this to work with multiple divs! i’ve been playing around with everything.. trying to rename variables… i can not figure it out- Eric or anyone else.. can you offer some advice?
aha! i found it! a basic modification of the script.. that i thought i had tried creating myself- but guess i had something wrong…
http://bytes.com/topic/javascript/answers/851117-one-ajax-script-check-one-file-if-something-change-do-refresh-other-file#post3413737
once you use that JS script.. just make sure you add the additional ‘onload’ function:
onload=”ajax_update_div1(); ajax_update_div2()”
goodluck
All of these AJAX/PHP scripts work great when you’re getting the time. But what about loading a page that’s calling data from a database? I’ve yet to find a script that can overcome the caching problem. Got any ideas on that? I’ve had no problem with the ASP equivelent, but my PHP version always caches the page. I have to load the page in a separate window and refresh it to get it to auto refresh inside the AJAX code.
It’s works! After you follow this step:
step 1: After download the zip file you must rename the “ajax_javascript.js” to “xmlhttp.js”.
step:2 Change all code at file “test.html” with this code:
PHP + Ajax auto refresh DIV
Loading….
Nice.
EDIT : STEP 2:
step2: Change all code at file “test.html” with File test.html above.
good job …. !. it’s work . you just check the test.html file.
replace this code : Loading….
with :
Loading….
thanks in advanced
good job …. !. it’s work . you just check the test.html file.
replace this code :
‘Loading….’
with :
‘Loading….’
replace oid with div
thanks in advanced
where is step2 edited? i can’t see it
Hi
This code is ok. Only dishonour slovak diacritic marks. How this shall change please?
Thanks for the script. I have included it in a cgi I have written. I’m using it to auto refresh ajax content and it works great.
- kris
Work like charm.. nice..
i change
setTimeout(‘ajax_update()’, 20000); to
setTimeout(‘ajax_update()’, 200);
Hi, the script work fine, just I change…
this:
Loading….
for this
Loading….
this is the problem:
<div id
best regards
T.
I put the summary of how I had it work here to help those after me.
1) the downloaded files are messed up so copy the code posted on top there are 3 files in total, but you will need only 2 either a combination of File xmlhttp.js and test.html or File xmlhttp.js and File ajax_php.php , it is up to you want html page or php page respectively.
2) Make sure the path and the xmlhttp.js file on test.html or ajax_php.php are set and named correctly, and make sure the characters (‘) and (“) are proper in the xmlhttp.js file.
3) load the page and you will see the result changing after a preset second on this line setTimeout of xmlhttp.js file.
4) Say thanks to those who help you …