what is on the menu today ?
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
steve
August 5th, 2008 at 2:02 pm
this is all i get:
Array
(
[i] => 1
)
1217961577
where the numbers increase
admin
August 5th, 2008 at 9:18 pm
can you show me your source code?
Eric
August 14th, 2008 at 3:24 pm
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]
Eric
August 14th, 2008 at 4:27 pm
Never mind! I figured out!!
Thanks for the code, it’s exactly what I needed!
ap
August 22nd, 2008 at 7:37 am
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!
admin
August 22nd, 2008 at 10:23 pm
can you download the zip files and test again?
Marlon Daley
October 5th, 2008 at 10:26 pm
In the download file some of the characters in the code changes, but even when i change them it still does not work.
Simba
October 8th, 2008 at 4:17 pm
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