Ajax注册代码

js代码
var xmlhttp;
//创建XmlHttpRequest对象
function createXmlHttpRequest()
{
if (window.ActiveXObject)
{
try
{
xmlhttp =new ActiveXObject ("Msxml2.XMLHTTP");//IE老版本
}
catch (e)
{}
try
{
xmlhttp =new ActiveXObject ("Microsoft.XMLHTTP");//IE新版本
}
catch (e)
{}
}
else if (window.XMLHttpRequest)
{
try
{
xmlhttp =new createXmlHttpRequest();//mozilla浏览器
}
catch (e)
{}
}
else
{
window.alert ("不能创建XmlHttpRequest对象");
return false ;
}
}
//检查用户名是否存在
function CheckName(name)
{
createXmlHttpRequest ();
var url="dispostEvn.aspx?name="+name+"&Evn=checkname";
xmlhttp.open("post",url,true );
xmlhttp.onreadystatechange=CheckUserName;
xmlhttp.send(null);
}
//检查用户名的回调函数
function CheckUserName()
{
if (xmlhttp.readystate==4)//判断对象状态
{
if (xmlhttp.status==200)//信息成功返回,开始处理信息
{
if (xmlhttp.responseText=="true")
{
alert ("用户已存在!");
document.getElementById("reg").disabled =true;

}
else
{
document.getElementById("reg").disabled =false;
}
}
}
}

function InsertName(name,pwd)
{
if (document.getElementById ("name").value=="")
{
window.alert ("用户名不能为空!");
return false ;
}
if ( document.getElementById ("pwd").value=="")
{
window.alert ("用户密码不能为空!");
return false ;
}
createXmlHttpRequest ();
var url="dispostEvn.aspx?name="+name+"&password="+pwd +"&Evn=InserName";
xmlhttp.open("get",url ,true );
xmlhttp.onreadystatechange=InsertUserName;
xmlhttp.send (null);
}
function InsertUserName()
{
if (xmlhttp.readystate==4)
{
if (xmlhttp.status==200)
{
if (xmlhttp.responseText=="true")
{
window.alert ("用户注册成功!");
document.getElementById ("name").value="";
document.getElementById ("pwd").value="";
}
else
{
window.alert ("用户注册失败!");
}
}
}
}

html代码

<input id="name" type="text" onblur ="CheckName(document.getElementById ('name').value)"/></td>
<td class="style8">
 
</td>
</tr>
<tr>
<td class="style2">
 
密码:</td>
<td class="style1">
 
<input id="pwd" type="password" />
</td>
<td>
 
</td>
</tr>
</table>
 
<input id="reg" type="button" value="注册" onclick ="InsertName(document.getElementById('name').value,document.getElementById('pwd').value)" />

处理页代码
public partial class dispostEvn : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["Evn"].ToString() == "checkname")
{
string name = Request.QueryString["name"].ToString();
SqlConnection con = GetCon();
SqlCommand cmd = new SqlCommand("CheckName", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@name", SqlDbType.VarChar, 20);
cmd.Parameters["@name"].Value = name;
con.Open();
int i = (int)cmd.ExecuteScalar();
con.Close();
if (i > 0)
{
Response.Write("true");
Response.End();
}
else
{
Response.Write("false");
Response.End();
}

}
if (Request.QueryString["Evn"].ToString() == "InserName")
{
string name = Request["name"].ToString();
string password = Request["password"].ToString();
SqlConnection con= GetCon();
SqlCommand cmd = new SqlCommand("InsertName",con );
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@name",SqlDbType.VarChar ,20);
cmd.Parameters["@name"].Value = name;
cmd.Parameters.Add("@password",SqlDbType.VarChar ,20);
cmd.Parameters["@password"].Value = password;
con.Open();
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
Response.Write("true");
Response.End();
}
else
{
Response.Write("false");
Response.End();
}
con.Close();
}
}
public SqlConnection GetCon()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings ["connStr"].ConnectionString );
return con;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值