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

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

如图:

 

 

文件包括:

  • userreg.html ( 注册页面)
  • ajaxreg .js(AJAX脚本及实时验证的JS脚本)
  • checkuserreg .php(连接数据库并检测用户名是否已注册的页面)
  • senduserinfo.php(信息合法后我们提交注册信息实现无刷新注册的PHP代码)

    代码如下:(只贴上关键部分以便理解)

    userreg.html ( 注册页面)
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script language="javascript" src="ajaxreg.js"></script>
<script language="JavaScript" type="text/JavaScript">
function check(){    //用户名为空的时候
    if(document.reg.username.value==""){
    document.getElementById('check').innerHTML=" <font color=red>用户名不能为空!</font>";
         document.reg.username.focus();
         return false;
    }
    if(document.getElementById('check').innerHTML==" <font color=red>The number is registed</font>"){    //用户名已被注册的时候(<font color=red>The number is registed</font>是AJAX返回回来的)
    document.reg.username.focus();
         return false;
    }
     if(document.reg.userpwd.value==""){     //密码为空的时候
           document.getElementById('pwd').innerHTML=" <font color=red>用户密码不能为空!</font>";
           document.reg.userpwd.focus();
           return false;
         }
         if(document.reg.userpwd.value.length<6){    //密码长度错误的时候
           document.getElementById('pwd').innerHTML=" <font color=red>密码长度不能小于6位!</font>";
           document.reg.userpwd.focus();
           return false;
         }

         if (document.reg.reuserpwd.value==""){ //没有再次输入密码的时候
           document.getElementById('repwd').innerHTML=" <font color=red>请再输入一次密码!</font>";
           document.reg.reuserpwd.focus();
           return false;
         }
         if(document.reg.userpwd.value!=document.reg.reuserpwd.value){   //再次输入密码不正确的时候
           document.getElementById('repwd').innerHTML=" <font color=red>两次输入的密码不一致!</font>";
          document.reg.reuserpwd.focus();
           return false;
         }
         return true;   //上面的验证都通过则return true,页面信息将被POST到checkuserreg.php进行处理。
}
</script>
</head>

<body>

         <form method="post" name="reg" id="reg" action="checkuserreg.php">
                 <table cellpadding="0" cellspacing="0" border="0" width="100%">
                         <tr>
                                 <td class="td1">登陆帐号:</td>
                                 <td>
                                         <input name="username" type="text" id="username" size="20" maxlength="20" class="textipt"/>
                                         <span id="check" class="msg">4-16个字符,英文小写、汉字、数字。</span></td>
                         </tr>
                         <tr>
                                 <td class="td1">登录密码:</td>
                                 <td><input name="userpwd" type="password" id="userpwd" size="30" maxlength="30" class="textipt"/>
                                   <span class="msg" id="pwd">密码字母有大小写之分,英文、数字。</span></td>
                         </tr>
                         <tr>
                                 <td class="td1">确认密码:</td>
                                 <td><input name="reuserpwd" type="password" id="reuserpwd" size="30" maxlength="30" class="textipt"/> <span class="msg" id="repwd">请再次输入登录密码。</span></td>
                         </tr>
                         <tr>
                                 <td colspan="2" align="left" valign="middle" height="50">
                 <input name="submit" type="image" src="img/reg_but1.jpg" /></td>
                         </tr>
                 </table>
                 </form>
</body>
</html>


ajaxreg .js代码(AJAX脚本及实时验证的JS脚本)

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


checkuserreg .php代码:(连接数据库并检测用户名是否已注册的页面)

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

//----------------------------------------------------------------------------
else{
echo" <font color=red>此会员名可以注册!</font>";
}
if(action==reg){
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();//关闭数据库连接 
?>


senduserinfo.php(信息合法后我们提交注册信息实现无刷新注册的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.

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值