如果不了解ajax,可以找一些教程看看。其实很简单。就是通过xmlhttp组建处理提交的条件数据,然后后台asp/asp.net程序根据条件数据查询数据库,得到不同结果。然后返回给前台js程序,js程序处理后无刷新的展现在前台。
1.ajaxnamecheck.js---这个文件包含到你的注册页面
//建立xmlHttp对象
function createXMLHttpRequest() {
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
}
function findNames() {//传递参数
createXMLHttpRequest();
var url = "findname.asp?names=" + document.getElementById("username").value;
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = callback;
xmlHttp.setRequestHeader("If-Modified-Since","0");//清楚ie缓存
xmlHttp.send(null);
}
function callback() {//回调函数
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200)
{
//获得返回数据
var the_names=xmlHttp.responseXML.getElementsByTagName("name")[0].firstChild.data;
clearNames();//清除旧数据
switch (the_names)//开始根据返回数据的不同情况填充不同内容
{
case "IsError":document.getElementById("checkna").innerHTML=" <font color=/"#FF3333/">* (用户名不能为中文,或者包含特殊字母)</font>";document.getElementById("adduser").disabled=true;break;
case "IsFalse":document.getElementById("checkna").innerHTML=" <font color=/"#FF3333/">*</font> <font color=/"#008000/">(恭喜,此用户名可用)</font>";document.getElementById("adduser").disabled=false;break;
case "IsTrue":document.getElementById("checkna").innerHTML=" <font color=/"#FF3333/">* (此用户名已经存在,请更换其他用户名)</font>";document.getElementById("adduser").disabled=true;break;
case "IsKong":document.getElementById("checkna").innerHTML=" <font color=/"#FF3333/">* (用户名不能为空)</font>";document.getElementById("adduser").disabled=true;break;//最后如果用户名为空,注册按钮降禁止添加。
}
}
else if (xmlHttp.status == 204)//错误提示
{
alert("发生错误,请联系网站技术人员!");
}
}
}
function clearNames()//清除旧内容
{
document.getElementById("checkna").innerHTML="";
}
?
2.注册页面,这里我只写一个用户名的input
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script language="javascript" src="ajaxnamecheck.js"></script>
<!--这里引用ajaxnamecheck.js注意你的路径,我的是和注册文件在同一个目录下-->
</head>
<body>
<table width="700" border=1 align="center" cellPadding=5 cellSpacing=0 bgcolor="#CCCCCC" bordercolorlight="#cccccc"bordercolordark="#ffffff">
<form method="post" action="a2uds0d0er8.html" onSubmit="return regcheck(this);" name="FormUserRegister">
<tr class="zd_td">
<td width="21%" height="30" align="right" bgcolor="#F7F7F7">帐 号:</td>
<td width="79%" height="30" bgcolor="#F7F7F7">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="34%"><input name="username" type="text" id="username" size="30" maxlength="20" onBlur="findNames()"></td>
<td width="66%"><div id="checkna"> <font color="#FF3333">*</font> (只能为中英文、数字、下划线 5-20个字符)</div></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
</body>
</html>
?
3.后台数据库处理程序
<!--#include virtual="Connections/conn.asp" -->
<%
username=request.QueryString("names")
if username="" then
Response.ContentType="text/xml"
response.Write "<?xml version=""1.0"" encoding=""GB2312"" ?>"
response.Write "<response>"
response.Write "<content>"
response.Write "<name>IsKong</name>"
response.Write "</content>"
response.Write "</response>"
else
if InStr(username,"or")<>0 or InStr(username,"'")<>0 or InStr(username,"`")<>0 or InStr(username,"=")<>0 or InStr(username,",")<>0 or InStr(username,"and")<>0 or InStr(username,"%")<>0 or InStr(username,"<")<>0 or InStr(username,">")<>0 or InStr(username,"&")<>0 or InStr(username,"-")<>0 or InStr(username,"@")<>0 or InStr(username,"*")<>0 or InStr(username,"mid")<>0 or InStr(username,"exec")<>0 or InStr(username,"insert")<>0 or InStr(username,"select")<>0 or InStr(username,"delete")<>0 or InStr(username,"update")<>0 then
Response.ContentType="text/xml"
response.Write "<?xml version=""1.0"" encoding=""GB2312"" ?>"
response.Write "<response>"
response.Write "<content>"
response.Write "<name>IsError</name>"
response.Write "</content>"
response.Write "</response>"
else
sql="select username from users where username='"&username&"'"
set rs=server.CreateObject("adodb.recordset")
rs.open sql,conn,1,1
if rs.eof then
Response.ContentType="text/xml"
response.Write "<?xml version=""1.0"" encoding=""GB2312"" ?>"
response.Write "<response>"
response.Write "<content>"
response.Write "<name>IsFalse</name>"
response.Write "</content>"
response.Write "</response>"
else
Response.ContentType="text/xml"
response.Write "<?xml version=""1.0"" encoding=""GB2312"" ?>"
response.Write "<response>"
response.Write "<content>"
response.Write "<name>IsTrue</name>"
response.Write "</content>"
response.Write "</response>"
end if
rs.close
set rs=nothing
end if
end if
%>