I recently try to learn Ajax. But my code never got pass the xmlhttp.readyState because it is always = to 1. My callback function is OnData and is call when onreadystatechange activate.
Here is my code. Please help
[CODE]
<?php
function get_message(){
return “Hello World! It is “.date(“r”);
}
if(isset($_GET[“NOW”])){
sleep(5);
header(“Cache-Control: no-cache, must-revalidate”);
header(“Content-type: text/plain”);
echo get_message();
}else {?>
<html>
<head>
<script type=”text/javascript”>
var xmlhttp = null;
function OnLoad(){
document.getElementById(“action”).href=”javascript:Update()”;
}
function Update(){
if(xmlhttp!= null) return;
document.getElementById(“data”).innerHTML = “Loading”;
xmlhttp = NewHTTP();
xmlhttp.open(“GET”, “?NOW”, false);
xmlhttp.onreadystatechange = OnData;
xmlhttp.send(null);
}
function NewHTTP(){
try{
return new XMLHttpRequest();
}catch(e){
return new ActiveXObject(“Msxml2.XMLHTTP.3.0”);
}
}
function OnData(){
m = document.getElementById(“data”);
if(xmlhttp.readyState == 4){
document.write(“inside the first if”);
if(xmlhttp.status == 200){
m.innerHTML = xmlhttp.responseText;
}else{
m.innerHTML = “Error loading date and time”;
}
}
xmlhttp = null;
}
</script>
</head>
<body onLoad = “OnLoad();”>
<div id=”data”>
<?
if(isset($_GET[“NOW-INLINE”])){
echo get_message();
}else
echo “Press Go”;
?>
</div>
<a href=”?NOW-INLINE” id=”action”>Go</a>
</body>
</html>
<?php } ?>