PHP 无刷新 注册

js的

var xmlHTTP;
//检测用户名是否存在
function CheckName(userName)
{
  createXMLHTTP();
   
  var url = "rh_userRegEvent.aspx?name='"+userName+"'&Event=check";
   
  xmlHTTP.Open("GET",url,true);
   
  xmlHTTP.onreadystatechange=checkUserName;
   
  xmlHTTP.send(null);
}



// 注册用户
function regUser(userName,userPwd)
{
  createXMLHTTP();
   
  userName = document.getElementById("userName").value;
  userPwd = document.getElementById("userPwd").value;
   
  if(userName=="")
  {
  window.alert("用户名不能为空!");
   
  document.getElementById("userName").focus();
   
  return false;
  }
  if(userPwd=="")
  {
  window.alert("密码不能为空!");
   
  document.getElementById("userPwd").focus();
   
  return false;
  }
   
  var url = "rh_userRegEvent.aspx?name='"+userName+"'&pwd='"+userPwd+"'&Event=Reg";
   
  xmlHTTP.Open("GET",url,true);
   
  xmlHTTP.onreadystatechange=regUserInfo;
   
  xmlHTTP.send(null);
}


function createXMLHTTP()
{
  if(window.XMLHttpRequest)
  {
  //xmlHTTP = new XMLHttpRequest();
   
  xmlHTTP = new ActiveXObject("Microsoft.XMLHTTP");
  }
   
  else if(window.ActiveXObject)
  {
  try
  {
  xmlHTTP = new ActiveXObject("Msxml2.XMLHTTP");
  }
  catch(e)
  {
  }
   
  try
  {
  xmlHTTP = new ActiveXObject("Microsoft.XMLHTTP");
  }
  catch(e)
  {
  }
  }
// if (typeof(xmlHttp) == "undefined")
// {
// alert("不能创建XMLHttp对象实例!");
// return false;
// }
}


// 回调函数
function checkUserName()
{
  if(xmlHTTP.readystate==4)
  {
  if(xmlHTTP.status==200)
  {  
  if(xmlHTTP.responseText == "true")
  {
  document.getElementById("imgName").src = "../images/icon/true.gif";
   
  document.getElementById("btnReg").disabled = false;
  }
   
  if(xmlHTTP.responseText == "false")
  {
  document.getElementById("imgName").src="../images/icon/false.gif";
   
  document.getElementById("btnReg").disabled=true;
  }
  }  
  }
}

//回调函数

function regUserInfo()
{
  if(xmlHTTP.readystate==4)
  {
  if(xmlHTTP.status==200)
  {
  if(xmlHTTP.responseText=="true")
  {
  document.getElementById("userName").value = "";
   
  document.getElementById("userPwd").value = "";
   
  window.alert("注册成功!!");
  }
   
  if(xmlHTTP.responseText=="false")
  {
  window.alert("注册失败!!");
  }
  }
  }
}


主界面的

<head runat="server">
  <title>无标题页</title>
   
  <script src="../js/CreateObject.js" type="text/javascript"></script>
   
  <link rel="Stylesheet" type="text/css" href="../css/css.css" />
   
</head>
<body onload="document.getElementById('userName').focus()">

  <form id="form1" runat="server">
  <div>
   
  <div style="margin-top:20px" align="center">无刷新的用户注册</div><br /><br />
   
  <table align="center" style=" 442px; height: 145px">
  <tr>
  <td style=" 100px">
  用户名:
  </td>
  <td style=" 100px">
  <input id="userName" type="text" onkeyup="CheckName(document.getElementById('userName').value);" /></td>
  <td style=" 100px">
  <img src="" id="imgName" alt=""/></td>
  </tr>
  <tr>
  <td style=" 100px">
  密码:</td>
  <td style=" 100px">
  <input id="userPwd" type="text" /></td>
  <td style=" 100px">
  </td>
  </tr>
  <tr>
  <td colspan="3">
  <input id="btnReg" style=" 69px" type="button" value="注册" onclick="regUser();" /></td>
  </tr>
  </table>
  </div>
  </form>
</body>


请求界面的[color=#FF0000][/color]
User user = new User();

  if (Request.QueryString["Event"].ToString() == "check")
  {
  if (user.checkName(Request.QueryString["name"].ToString()))
  {
  Response.Write("true");

  Response.End();
  }
  else
  {
  Response.Write("false");

  Response.End();
  }
  }
  if (Request.QueryString["Event"].ToString() == "Reg")
  {
  if (user.regUser(Request.QueryString["name"].ToString(), Request.QueryString["pwd"].ToString()))
  {
  Response.Write("true");

  Response.End();
  }
  else
  {
  Response.Write("false");

  Response.End();
  }
  }


还有一个类

public class User
{
public User()
{
//
//TODO: 在此处添加构造函数逻辑
//
}

  public ArrayList getUser(string userName)
  {
  SqlConnection con = SqlConString.getConnnectionString();

  con.Open();

  string sql = "select UserName from tUser where UserName = '" + userName + "'";

  SqlCommand cmd = new SqlCommand(sql, con);

  SqlDataReader dr = cmd.ExecuteReader();

  ArrayList al = new ArrayList();

  while (dr.Read())
  {
  al.Add(dr["UserName"].ToString());
  }

  dr.Close();

  con.Close();

  return al;
  }

  public bool checkName(string userName)
  {
  SqlConnection con = SqlConString.getConnnectionString();

  con.Open();
   
  string sql = "select * from tUser where UserName = " + userName + "";
   
  SqlDataAdapter sda = new SqlDataAdapter(sql,con);

  DataSet ds = new DataSet();

  sda.Fill(ds);

  int m = ds.Tables[0].Rows.Count;
   
  if (m <= 0)
  {
  return true;
  }
  else
  {
  return false;
  } 
  }

  public bool regUser(string userName, string userPwd)
  {
  SqlConnection con = SqlConString.getConnnectionString();

  con.Open();

  SqlCommand cmd = new SqlCommand("insert into tUser(UserName,UserPwd) values (" + userName + "," + userPwd + ")", con);

  int i = cmd.ExecuteNonQuery();

  if (i > 0)
  {
  return true;
  }
  else
  {
  return false;
  }
  }
}

原文地址:https://www.cnblogs.com/visi_zhangyang/p/2640174.html