首页 > 编程 > PHP > 正文

PHP+AJAX实现无刷新注册(带用户名实时检测)

2019-11-22 01:42:21
字体:
来源:转载
供稿:网友
很多时候,我们在网上注册个人信息,在提交完页面后,总得等待页面刷新来告诉我们注册是否成功,遇到网络差的时候,如果注册了一大串的东西,在经过漫长的等待页面刷新后,得到的确是“您的用户名已被使用”或XXXXXXX不合法,我想大家的心情一定特别不爽,今天就介绍个AJAX实现页面不刷新注册+实时检测用户信息的简单注册程序,希望对大家有所帮助。好的,先看注册界面代码:

  <table width="831" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr style="display:none">
    <td height="35" align="center" id="result"> </td>
  </tr>
</table>
<table width="100%" height="256" border="0" align="center" cellpadding="1" cellspacing="1">
      <tr>
        <td width="150" align="left" bgcolor="#FFFFFF">  ・ 用户名称:          </td>
        <td width="310" align="center" bgcolor="#FFFFFF"> 
          <input name="username" type="text" class="inputtext" id="username" onChange="usercheck('check')" onBlur="usercheck('check')">
        <font color="#FF6633">*</font></td>
        <td align="left" bgcolor="#FFFFFF" id="check"> 4-16个字符,英文小写、汉字、数字、最好不要全部是数字。</td>
      </tr>
      <tr>
        <td width="150" align="left" bgcolor="#FFFFFF">   ・ 用户密码:</td>
        <td width="310" align="center" bgcolor="#FFFFFF"> 
          <input name="userpwd" type="password" class="inputtext" id="userpwd" onChange="pwdcheck('pwd')" onBlur="pwdcheck('pwd')"
          <font color="#FF6633">*</font>        </td>
        <td align="left" bgcolor="#FFFFFF" id="pwd"> 密码字母有大小写之分。6-16位(包括6、16),限用英文、数字。</td>
      </tr>
   <tr>
        <td width="150" align="left" bgcolor="#FFFFFF">  ・ 重复密码:</td>
        <td width="310" align="center" bgcolor="#FFFFFF">  
          <input name="reuserpwd" type="password" class="inputtext" id="reuserpwd" onChange="pwdrecheck('repwd')" onBlur="pwdrecheck('repwd')"> 
           <font color="#FF6633">*</font>        </td>
        <td align="left" bgcolor="#FFFFFF" id="repwd"> 请再次输入登录密码。</td>
   </tr>
    </table>

如图:

 

红色部分就是一会要调用的js函数了,当我们选定一个文本框后就会开始调用,现在我们看上面那个页面包含的ajaxreg.js文件的代码,里面就是包含了ajax框架和一些判断函数。

var%20http_request=false;
 %20function%20send_request(url){//初始化,指定处理函数,发送请求的函数
   %20http_request=false;
 //开始初始化XMLHttpRequest对象
 if(window.XMLHttpRequest){//Mozilla浏览器
 %20http_request=new%20XMLHttpRequest();
 %20if(http_request.overrideMimeType){//设置MIME类别
   %20http_request.overrideMimeType("text/xml");
 %20}
 }
 else%20if(window.ActiveXObject){//IE浏览器
 %20try{
  %20http_request=new%20ActiveXObject("Msxml2.XMLHttp");
 %20}catch(e){
  %20try{
  %20http_request=new%20ActiveXobject("Microsoft.XMLHttp");
  %20}catch(e){}
 %20}
   %20}
 if(!http_request){//异常,创建对象实例失败
 %20window.alert("创建XMLHttp对象失败!");
 %20return%20false;
 }
 http_request.onreadystatechange=processrequest;
 //确定发送请求方式,URL,及是否同步执行下段代码
   %20http_request.open("GET",url,true);
 http_request.send(null);
 %20}
 %20//处理返回信息的函数
 %20function%20processrequest(){
  %20if(http_request.readyState==4){//判断对象状态
    %20if(http_request.status==200){//信息已成功返回,开始处理信息
  %20document.getElementById(reobj).innerHTML=http_request.responseText;
 %20}
 %20else{//页面不正常
  %20alert("您所请求的页面不正常!");
 %20}
  %20}
 %20}
 %20function%20usercheck(obj){
  %20var%20f=document.reg;
  %20var%20username=f.username.value;
  %20if(username==""){
  %20document.getElementById(obj).innerHTML=" <font%20color=red>用户名不能为空!</font>";
 f.username.focus();
 return%20false;
  %20}
  %20else{
  %20document.getElementById(obj).innerHTML="正在读取数据...";
  %20send_request('checkuserreg.php?username='+username);
  %20reobj=obj;
  %20}
 %20}
 %20function%20pwdcheck(obj){
   %20var%20f=document.reg;
 var%20pwd=f.userpwd.value;
 if(pwd==""){
  %20document.getElementById(obj).innerHTML=" <font%20color=red>用户密码不能为空!</font>";
  %20f.userpwd.focus();
  %20return%20false;
 }
 else%20if(f.userpwd.value.length<6){
  %20document.getElementById(obj).innerHTML=" <font%20color=red>密码长度不能小于6位!</font>";
  %20f.userpwd.focus();
  %20return%20false;
 }
 else{
  %20document.getElementById(obj).innerHTML=" <font%20color=red>密码符合要求!</font>";
 }
 %20}
 %20function%20pwdrecheck(obj){
   %20var%20f=document.reg;
 var%20repwd=f.reuserpwd.value;
 if%20(repwd==""){
  %20document.getElementById(obj).innerHTML=" <font%20color=red>请再输入一次密码!</font>";
  %20f.reuserpwd.focus();
  %20return%20false;
 }
 else%20if(f.userpwd.value!=f.reuserpwd.value){
  %20document.getElementById(obj).innerHTML=" <font%20color=red>两次输入的密码不一致!</font>";
  %20f.reuserpwd.focus();
  %20return%20false;
 }
 else{
  %20document.getElementById(obj).innerHTML=" <font%20color=red>密码输入正确!</font>";
 }
}

  %20不难看出,数据是通过异步传输到checkuserreg.php接着回送回信息显示出来来达到实时检测用户名的目的,至于密码,只作了长度的实时判断,有兴趣的朋友可以扩充功能。来看下checkuserreg.php到底都做了什么:

<?php
 %20header('Content-Type:text/html;charset=GB2312');//避免输出乱码
 %20include('inc/config.inc.php');//包含数据库基本配置信息
 %20include('inc/dbclass.php');//包含数据库操作类
 %20$username=trim($_GET['username']);//获取注册名
 %20//-----------------------------------------------------------------------------------
 %20$db=new%20db;//从数据库操作类生成实例
 %20$db->mysql($dbhost,$dbuser,$dbpassword,$dbname);//调用连接参数函数
 %20$db->createcon();//调用创建连接函数
 %20//-----------------------------------------------------------------------------------
 %20$querysql="select%20username%20from%20cr_userinfo%20where%20username='$username'";//查询会员名
 %20$result=$db->query($querysql);
 %20$rows=$db->loop_query($result);
 %20//若会员名已注册
 %20//-----------------------------------------------------------------------------------
 %20if($rows){
  %20echo" <font%20color=red>此会员名已被注册,请更换会员名!</font>";
 %20}
 %20//会员名未注册则显示 %20

 %20//----------------------------------------------------------------------------
 %20else{
  %20echo" <font%20color=red>此会员名可以注册!</font>";
 %20}
 %20if($action==reg){
 %20$addsql="insert%20into%20cr_userinfo
         %20values(0,'$username','$userpwd','$time',50,1,'$userquestion','$useranswer')";

 %20$db->query($addsql);
 %20echo"<img%20src=images/pass.gif> <font color=red>恭喜您,注册成功!请点击<a href=login.php>这里</a>登陆!</font>";
  }
  $db->close();//关闭数据库连接
?>

   注释写的还算详细,大家应该都能看懂,再看信息合法后我们提交注册信息实现无刷新注册的PHP代码,senduserinfo.php:

<?php
  header('Content-Type:text/html;charset=GB2312');//避免输出乱码
  include('inc/config.inc.php');//包含数据库基本配置信息
  include('inc/dbclass.php');//包含数据库操作类
  $username=trim($_GET['username']);//获取注册名
  $userpwd=md5(trim($_GET['userpwd']));//获取注册密码
  $time=date("Y-m-d");

  //-----------------------------------------------------------------------------------
  $db=new db;//从数据库操作类生成实例
  $db->mysql($dbhost,$dbuser,$dbpassword,$dbname);//调用连接参数函数
  $db->createcon();//调用创建连接函数
  //-----------------------------------------------------------------------------------
  //开始插入数据
  //-----------------------------------------------------------------------------------
  $addsql="insert into cr_userinfo values(0,'$username','$userpwd','$time',50,1,'$userquestion','$useranswer')";
  $db->query($addsql);
  echo"<img src=images/pass.gif> <font color=red>恭喜您,注册成功!请点击<a href=login.php>这里</a>登录!</font>";

  $db->close();//关闭数据库连接
?>

OK!!大功告成,来看看效果图:

1.

 

2.

 

3.

 

4.

 

5.

 

怎么样?还不错吧,贴了这么多累死了,希望大家喜欢~~~~

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表