/    Sign up×
Community /Pin to ProfileBookmark

readyState always = 1??? Why

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 } ?>
[/CODE]

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @KingdomHeart spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...