日期:2014-05-17  浏览次数:20986 次

使用xmlhttp检测表单与数据库是否重复.
离开文本框,马上检测文本框的数据是否与数据库里面重复,如果重复就提示出来.
但我不会AJAX,所以想用XMLHTTP.但是不行,没反应.请教大家了.

<body>
<div   id= "show_div "   align= "center "> 正在进行验证,请等待…… </div>  
<form   name= "form1 "   method= "post "   action= " ">  
<table   border= "1 "   cellspacing= "1 "   cellpadding= "0 "   align= "center ">  
<tr>    
<td   width= "88 "> <b> 姓名: </b> </td>  
<td   width= "200 ">    
<input   type= "text "   name= "FirstName "   >  
</td>  
</tr>  
<tr>    
<td> <b> 用户ID: </b> </td>  
<td> <input   type= "text "   name= "UserID "   onBlur= "test_id(this.value) "> </td>  
</tr>  

</table>  
<p   align= "center ">    
<input   type= "submit "   name= "Submit "   value= "注册 "   style= "font-size:9pt ">  
<input   type= "reset "   name= "Submit2 "   value= "填写 "   style= "font-size:9pt ">  
</p>  
</form>  
<SCRIPT   LANGUAGE= "JavaScript ">  
var   userid,   oXMLHTTP;  
function   test_id(suserid)  
{  
oXMLHTTP   =   new   ActiveXObject( "Microsoft.XMLHTTP ");  
userid   =   suserid;  
var   sURL   =   "http://localhost:8085/by/tempb.asp?userid= "   +   userid;  
oXMLHTTP.onreadystatechange   =   managestatechange;  
oXMLHTTP.open( "GET ",   sURL,   true);  
try{  
oXMLHTTP.send();  
}  
catch(e)  
{  
alert( "此时无法进行验证。 ");  
document.form1.FirstName.focus();  
}  
}  
 
function   managestatechange(){  
switch(oXMLHTTP.readyState)  
{  
case   2,   3:  
document.all( "show_div ").style.display   =   "block ";  
break;  
case   4:  
if(oXMLHTTP.responseText   ==   "exists ")    
alert( "很抱歉!   User   ID   :   "   +   userid   +   "已经存在。 ");  
document.all( "show_div ").style.display   =   "none ";  
break;  
}    
}  
document.all( "show_div ").style.display   =   "none ";  
</script>  
</body>


tempb.asp   (本来是调数据库的,但是方便测试,我就换了了这个)

<body>
<%  
sUserID   =   Replace(Trim(Request.QueryString( "userid ")), " ' ", " ")  
If   sUserID   =   "aa "   Then   Response.Write   "exists "    
%>  
</body>


------解决方案--------------------
function checkUsername()
{