index.php
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>ajax</title>
<script type="text/javascript" src="ajax.js"></script>
<script type="text/javascript">
function $(id){
return document.getElementById(id);
}
function checkName(){
var http_request=createAjax();
http_request.onreadystatechange=function(){
if(http_request.readyState==4 && http_request.status==200){
var doc=http_request.responseXML;
var nodeList=doc.getElementsByTagName("title");
$("notice").innerHTML=nodeList[0].childNodes[0].nodeValue;
}
}
var data="username="+$("username").value+"&mytime="+new Date();
var url="index_do.php";
http_request.open("post",url,true);
http_request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
http_request.send(data);
}
</script>
</head>
<body>
Please enter your username:<input type="text" name="username" id="username" onChange="checkName();" />
<span id="notice"></span>
</body>
</html>
index_do.php
<?php
header("Content-Type:text/xml;charset=utf-8");
header("Cache-Control:no-cache");
$username=$_POST['username'];
$info="";
if ($username=='admin'){
$info="<bookstore><title>骆驼祥子</title><author>老舍</author><title>狂人日记</title><author>鲁迅</author></bookstore>";
}else {
$info="<bookstore><title>子夜</title><author>矛盾</author><title>背影</title><author>朱自清</author></bookstore>";
}
echo $info;
?>
注意:
header("Content-Type:text/xml;charset=utf-8");