正在加载

ajax+php注册用户名验证

一般当我们在网站注册的时候,只有在提交后才验证用户名是否已被注册,这样页面会全部刷新后才可以再更换用户名。当然如果网速很快的话,那么就没多大问题,但如果速度慢的话,从提交到知道用户名已被注册再到返回到注册页面,这可能是很长的时间段,是不是很烦?

ajax+php来实现用户名验证的话,就方便多了。这在网上也可以搜到很多关于这方面的,只是最近在学ajax,所以还是自己写了下。

数据库简单,id,username

index.html代码:

<form action=”" method=”post” name=”form1″>
<table width=”67%” height=”144″ border=”1″ align=”center” cellpadding=”1″ cellspacing=”1″>
      <tr>     
        <td width=”107″ height=”49″ align=”left” bgcolor=”#FFFFFF”>  · 用户名称:          </td>
        <td width=”231″ align=”left” bgcolor=”#FFFFFF”>
          <input name=”username” type=”text” id=”username” >       
        <font color=”#FF6633″>*</font><input type=”button” value=”检测” onclick=”check();” /><br /><span id=”re” style=”color:#FF0000; font-size:14px; text-align:center;”></span></td>
        <td width=”477″ align=”left” bgcolor=”#FFFFFF” id=”check”> 4-16个字符,英文小写、汉字、数字、最好不要全部是数字。</td>
      </tr>                                      

</table>
</form>

ajaxreg.js代码:

// JavaScript Document
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;
        }
  }
  //处理返回信息的函数
  function processrequest(){
   if(http_request.readyState==4){//判断对象状态
     if(http_request.status==200){//信息已成功返回,开始处理信息
          document.getElementById(’re’).innerHTML=http_request.responseText;
         }
         else{//页面不正常
          alert(”您所请求的页面不正常!”);
         }
   }
  }
  function processrequest2(){
   if(http_request.readyState==4){//判断对象状态
     if(http_request.status==200){//信息已成功返回,开始处理信息
          document.getElementById(’wrong’).innerHTML=http_request.responseText;
          window.location.href=”http://www.baidu.com”;  //注册成功后跳转到百度,可以自定义
         }
         else{//页面不正常
          alert(”您所请求的页面不正常!”);
         }
   }
  }
  function check(){
   var f=document.form1;
   var uname=f.username.value;
   if(uname==”"){
   document.getElementById(’re’).innerHTML=’用户名不能为空!’;
        f.username.focus();
        return false;
   }
   else{
   document.getElementById(’re’).innerHTML=’正在读取数据…’;
   var username=document.form1.username.value;
   var queryString=”username=”+username;
   send_request();
   http_request.open(”POST”,”checkuserreg.php”,true);
   http_request.onreadystatechange=processrequest;
    http_request.setRequestHeader(”Content-Type”,”application/x-www-form-urlencoded;”);
    //确定发送请求方式,URL,及是否同步执行下段代码
    //http_request.open(”GET”,url,true);
    http_request.send(queryString);
   }
  }

checkuserreg.php代码:

<?php
  header(’Content-Type:text/html;charset=GB2312′);//避免输出乱码
  $dbhost     = “localhost”;
  $dbuser     = “root”;
  $dbpassword = “123″;
  $dbname     = “user”;
  mysql_connect($dbhost,$dbuser,$dbpassword) or die(”error!”);
  mysql_query(”set names ‘gbk’”);
  mysql_select_db($dbname);
  $username=trim($_POST['username']);//获取注册名
  $sql=”select username from user where username=’$username’”;//查询会员名
  $result=mysql_query($sql);
  $num=mysql_num_rows($result);
  //$rows=mysql_fetch_array($result);
  if($num<>0){
          echo “此会员名已被注册,请更换会员名!”;
  }
  else{
          echo “此会员名可以注册!”;
  }
  mysql_close();//关闭数据库连接
?>

发表评论