"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
-<script language="javascript" type="text/javascript">
+ <title>AJAX example</title>
+<script type="text/javascript">
+var httpObject = getHTTPObject();
+
function getWord() {
return document.getElementById('word').value;
}
function setDef(text) {
- document.getElementById('def').innerHTML = text;
+ /* HTML way */
+ /* document.getElementById('def').innerHTML = text; */
+ /* XHTML way */
+ var text_node = document.createTextNode(text);
+ var p = document.getElementById('def');
+ p.replaceChild(text_node, p.childNodes[0]);
}
function getHTTPObject() {
if (window.ActiveXObject)
}
}
function doAJAX() {
- httpObject = getHTTPObject();
if (httpObject != null) {
httpObject.open("GET", "lookup.php?word=" + getWord(), true);
- httpObject.send(null); // request is on it's way...
- setDef("Sent request for" + getWord());
- // here's the asynchronous read:
+ httpObject.send(null); /* request is on it's way... */
+ setDef("Sent request for " + getWord());
+ /* here's the asynchronous read: */
httpObject.onreadystatechange = function() { respondAJAX(); }
}
}
function respondAJAX(){
if(httpObject.readyState == 4) {
- setDef(httpObject.responseText);
+ var $xml = httpObject.responseXML;
+ var $text = $xml.getElementsByTagName('result')[0].childNodes[0].nodeValue;
+ setDef($text);
}
}
</script>
<body>
<form action="never_call_this_invalid_page.html" method="get">
<p>
- Word: <input type="text" name="word" id="word" onBlur="doAJAX();"/><br />
- <input type="submit" value="Submit" />
+ Word: <input type="text" name="word" id="word" onblur="doAJAX();"/><br />
+ Click somewhere else to lookup word.
</p>
- <div name="def" id="def"></div>
+ <p id="def"> </p>
</form>
</body>
</html>
foreach ($f as $entry) {
if ($entry->word == $word) {
$ret .= '<result>'.$entry->definition.'</result>';
- // this doesn't even have to be XML, but for official "AJAX"
+ // this doesn't even have to be XML, but we're doing official "AJAX"
break;
}
}
if (strlen($ret) == 0) {
$ret .= '<result>'.$word.' not found</result>';
}
-echo '<?xml version="1.0" encoding="UTF-8" ?>';
+
+header("Content-Type: text/xml; charset=UTF-8");
+echo '<'.'?xml version="1.0" encoding="UTF-8" ?'.">\n";
echo $ret;
?>