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">
                    &nbsp;
                </td>
            </tr>
            <tr>
                <td class="style2">
                    &nbsp;
                    密码:</td>
                <td class="style1">
                    &nbsp;
                    <input id="pwd" type="password" />
                </td>
                <td>
                    &nbsp;
                </td>
            </tr>
        </table>
        &nbsp;      
        <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;
    }

你可能感兴趣的:(Ajax,UI,浏览器,IE,Microsoft)