文件 reg.php
<html>
<head><title>用戶註冊</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
</head>
<body>
<form action="???" method="post">
用戶名字:<input type="text" onkeyup="checkName();" name="username1" id="username">
<input style="border-width: 0;color: red;" type="text" id="myresult" value="">
<br/>
用戶密碼:<input type="password" name="password"><br>
電子郵件:<input type="text" name="email"><br/>
<input type="submit" value="用戶註冊">
</form>
<form action="???" method="post">
用戶名字:<input type="text" name="username2" >
<br/>
用戶密碼:<input type="password" name="password"><br>
電子郵件:<input type="text" name="email"><br/>
<input type="submit" value="用戶註冊">
</form>
</body>
<script language="javascript">
var xmlHttpRequest; //xml對象變量
var myre=document.getElementById("myresult");
//不同的瀏覽器獲取對象xmlhttprequest 對象方法不一樣
if(window.ActiveXObject){xmlHttpRequest=new ActiveXObject("Microsoft.XMLHTTP");
}else{
xmlHttpRequest=new XMLHttpRequest();
}
function checkName(){
var name=document.getElementById("username");
if(xmlHttpRequest){
//通過xmlHttpRequest對象發送請求到服務器的某個頁面
//第一個參數表示請求的方式, "get" / "post"
//第二個參數指定url,對哪個頁面發出ajax請求(本質仍然是http請求)
//第三個參數表示 true表示使用異步機制,如果false表示不使用異步
var url="registerPro.php?"+"&username="+name.value;
//打開請求.
xmlHttpRequest.open("get",url,true);
//指定回調函數.chuli是函數名
xmlHttpRequest.onreadystatechange=chuli;
//真的發送請求,如果是get請求則填入 null即可
//如果是post請求,則填入實際的數據
}
}
function chuli(){
//window.alert("處理函數被調用"+myXmlHttpRequest.readyState);
//取出從registerPro.php頁面返回的數據
myre.value=xmlHttpRequest.responseText;
}
}
</script>
</html>
文件regjudge.php
<?php
//這裏兩句話很重要,第一講話告訴瀏覽器返回的數據是xml格式
header("Content-Type: text/xml;charset=utf-8");
//告訴瀏覽器不要緩存數據
header("Cache-Control: no-cache");
//接收數據
$username=$_GET['username'];
if($username=="張三"){
echo "用戶名不可以用";//注意,這裏數據是返回給請求的頁面.
}else{
echo $username."用戶名可以用";
}