//定義XMLHttpRrequest對象
var xmlHttp=createXmlHttpRequestObject();
//獲取XMLHttpRrequest對象
function createXmlHttpRequestObject(){
//用來存儲將要使用的XMLHttpRrequest對象
var xmlHttp;
//若是在internet Explorer下運行
if(window.ActiveXObject){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){
xmlHttp=false;
}
}else{
//若是在Mozilla或其餘的瀏覽器下運行
try{
xmlHttp=new XMLHttpRequest();
}catch(e){
xmlHttp=false;
}
}
//返回建立的對象或顯示錯誤信息
if(!xmlHttp)
alert("返回建立的對象或顯示錯誤信息");
else
return xmlHttp;
}
//使用XMLHttpRequest對象建立異步HTTP請求
function process(){
//在xmlHttp對象不忙時進行處理
if(xmlHttp.readyState==4 || xmlHttp.readyState==0){
//獲取用戶在線表單中輸入的姓名
names = document.getElementById("username").value;
tels = document.getElementById("tel").value;
addresss =document.getElementById("address").value;
//在服務器端執行quickstart.php
xmlHttp.open("GET","zhuce_ok.php?online_user="+names+"& online_tel="+tels+"& online_address="+addresss,true);
//定義獲取服務器端響應的方法
xmlHttp.onreadystatechange=handleServerResponse;
//向服務器發送請求
xmlHttp.send(null);
}else
//若是服務器忙,1秒後重試
setTimeout('process()',1000);
}
//當收到服務器端的消息時自動執行
function handleServerResponse(){
//在處理結束時進入下一步
if(xmlHttp.readystate==4){
//狀態爲200表示處理成功結束
if(xmlHttp.status==200){
//獲取服務器端發來的XML信息
xmlResponse=xmlHttp.responseXML;
//獲取XML中的文檔對象(根對象)
xmlDocumentElement=xmlResponse.documentElement;
//獲取第一個文檔子元素的文本信息
helloMessage=xmlDocumentElement.firstChild.data;
//使用從服務器端發來的消息更新客戶端顯示的內容
document.getElementById("divMessage").innerHTML='<i>'+helloMessage+'</i>';
//從新開始
setTimeout('process()',1000);
}else{
//若是HTTP的狀態不是200表示發生錯誤
alert("There was a problem accessing the server:"+xmlHttp.statusText);
}
}
}
zhuce_ok.php:
<?php
//建立一個XML格式輸出
header('Content-Type: text/xml');
//建立XML頭
echo '<?xml version="1.0" encoding="gb2312" standalone="yes" ?>';
//建立<response>元素
echo '<response>';
//獲取用戶姓名
$online_tel=$_GET[online_tel];
$online_address=$_GET[online_address];
$online_number=substr(mt_rand(100000,999999),0,6);
$online_pass=substr(mt_rand(100000,999999),0,6);
//根據從客戶端獲取的用戶建立輸出
include("conn/conn.php");
$query=mssql_query("insert into tb_user(online_user,online_tel,online_address,online_number,online_pass) values('$online_user','$online_tel','$online_address','$online_number','$online_pass')");
if($query==true){
echo $online_user=$_GET[online_user];
echo "用戶註冊成功,這是您的准考證號碼$online_number.和密碼$online_pass.";
}
//關閉<response>元素
echo '</response>';
?>