Ive copied an online example for writing out a php file, programmatically,
then would like to re-display that data in a browswer window that
automatically refreshes as the data file (getdata.php, which is the file I
am programmatically rewriting to elsewhere) gets changed.
I am trying to use AJAX and JSON to do this. I have copied an example of
using HttpRequest Object as the backbone of this from
http://www.w3schools.com/dom/dom_http.asp. Further, I am enclosing both of
my files here, in full as opposed to mere snippets. This code does what I
wish it to do except:
1. It does not update when getdata.php is rewritten. On non MSIE broswers,
it shows when first invoked, but not subsequently.
2. It fails altogether under MSIE (6.026 SP1 || SP2) -- I never get
xmlhttp.readyState==4 under MSIE.
Hoping someone can point me in the right direction here -- I think I almost
have this thing finished. -Ike
//
index.html//////////////////////////////////////////////////////////////////
////////////////////
<html>
<head>
<title>uplog</title>
<script type="text/javascript">
var xmlhttp;
function loadXMLDoc(url)
{
if (document.all){
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
}else{
xmlhttp=new XMLHttpRequest()
}
// code for Mozilla, etc.
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest()
xmlhttp.onreadystatechange=xmlhttpChange
xmlhttp.open("GET",url,true)
xmlhttp.send(null)
}
// code for IE
else if (window.ActiveXObject)
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
if (xmlhttp)
{
xmlhttp.onreadystatechange=xmlhttpChange
xmlhttp.open("GET",url,true)
xmlhttp.send()
}
}
}
function xmlhttpChange()
{
// if xmlhttp shows "loaded"
if (xmlhttp.readyState==4)
{
// if "OK"
if (xmlhttp.status==200)
{
var rows = eval(xmlhttp.responseText);
var html = "<table>";
for(r in rows){
html+="<tr>";
html+="<td>"+rows[r].username+"</td>";
html+="</tr>";
}
html+="</table>";
document.getElementById("data").innerHTML = html;
}
else
{
//alert("Problem retrieving XML data")
}
}
}
</script>
</head>
<body>
<div id="data">
</div>
<script>
loadXMLDoc('http://127.0.0.1/roomx1/getdata.php');
</script>
</body>
</html>
// getdata.php
////////////////////////////////////////////////////////////////////////////
////////////////////
<?php
ini_set('include_path',ini_get('include_path').";C :\Program Files\Apache
Group\Apache2\htdocs"."\pear");
require('JSON.php');
$records=array();
$records[]=array('order'=>1, 'username'=>'tommy');
$records[]=array('order'=>2, 'username'=>'ralph');
$records[]=array('order'=>3, 'username'=>'eod');
$records[]=array('order'=>4, 'username'=>'howard');
$json=new Services_JSON();
echo($json->encode($records));
?>