首页 > PHP > php+ajax 注册验证用户名是否存在实例

php+ajax 注册验证用户名是否存在实例

2009年05月09号
查看评论 发表评论 1,117次浏览

register.html

<script>
//创建ajax
function creatAjax()
{
var HttpRequest=false;
try {
HttpRequest=new XMLHttpRequest();
} catch(e) {
var arrXMLHTTP=["Msxml3.XMLHTTP","Msxml2.XMLHTTP","Microsoft.XMLHTTP"];
for(var i=0;i<arrXMLHTTP.length;i++) {
try {
HttpRequest=new ActiveXObject(arrXMLHTTP[i]);
} catch(e) {}
if(HttpRequest) break;
}
}
return HttpRequest;
}

//使用ajax
function useAjax(username){
var ajax = creatAjax();
strUrl = “register.php”
ajax.open(”POST”,strUrl,false);
ajax.setRequestHeader(”Content-Type”,”application/x-www-form-urlencoded”);
strPost=”username=”+username;
ajax.send(strPost);
var str = ajax.responseText;
if(str==1){
document.getElementById(’ajax’).innerHTML=’此会员名已被注册,请更换会员名!’;
}else{
document.getElementById(’ajax’).innerHTML=’此会员名可以注册!’;
} //alert(str);
return false;
}

</script>
<input type=”text” onblur=”useAjax(this.value)”><span id=”ajax”></span>

register.php

<?php
mysql_connect(’localhost’,'root’,”);
mysql_select_db(’ecshop_utf8′);
$strSql = mysql_query(”select user_name from ecs_users where user_name =’”.$_POST["username"].”‘”);
$line = mysql_fetch_array($strSql,MYSQL_ASSOC);

if($line["user_name"]){
echo “1″;
exit;
}else{
echo “0″;
exit;

}
?>
checkuserreg.html

<html>
<head>
<script language=”javascript”>// 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);
   }
}
</script>
<meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″></head>
<body>
<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” >      
          <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>
</body>
</html>

checkuserreg.php

<?php
header(’Content-Type:text/html;charset=GB2312′);//避免输出乱码
$dbhost     = “localhost”;
$dbuser     = “root”;
$dbpassword = “”;
$dbname     = “ecshop_utf8″;
mysql_connect($dbhost,$dbuser,$dbpassword) or die(”error!”);
mysql_query(”set names ‘gbk’”);
mysql_select_db(’ecshop_utf8′);

$username=trim($_POST['username']);//获取注册名
$sql=”select user_name from ecs_users where user_name=’$username’”;//查询会员名
$result=mysql_query($sql);
$num=mysql_num_rows($result);
$rows=mysql_fetch_array($result);
if($num<>0){
          echo “此会员名已被注册,请更换会员名!”;
}
else{
          echo “此会员名可以注册!”;
}
mysql_close();//关闭数据库连接
?>

类别PHP 标签
  1. 548
    发表于 2009年08月27号 06时30分00秒 | 1楼

    רҵ

  2. 203
    发表于 2009年12月03号 16时26分04秒 | 2楼

  3. txt小说免费下载
    发表于 2009年12月03号 22时33分00秒 | 3楼

    太不错了。顶一下

  4. 一咯
    发表于 2009年12月06号 23时37分15秒 | 4楼

    我来了,你这文章很不错喔。

  5. 854
    发表于 2009年12月10号 03时35分32秒 | 5楼

  6. QQ牧场外挂
    发表于 2009年12月10号 21时02分56秒 | 6楼

    已经达到了忘我的境界了呵呵。

  7. 优酷网看电影
    发表于 2009年12月11号 01时02分33秒 | 7楼

    博主的文采很OK。

  8. 889
    发表于 2009年12月28号 10时13分19秒 | 8楼

  9. yu
    发表于 2010年01月08号 19时29分46秒 | 9楼

    ljjjj

  10. LV handbags
    发表于 2010年06月26号 21时01分28秒 | 10楼

    Very much enjoyed.

  11. Cheap timberland boots
    发表于 2010年07月01号 13时27分46秒 | 11楼

    Have a great day.
    LOADS OF SUNSHINE TO BE ON YOU TODAY.

  12. womens timberland boots sale
    发表于 2010年07月10号 14时31分24秒 | 12楼

    I was completely astonished after reading the article.

  13. timberland 6 inch boots
    发表于 2010年07月27号 14时29分49秒 | 13楼

    Loved your comment. Thank you

  14. Herve Leger Sale
    发表于 2010年07月27号 16时56分04秒 | 14楼

    Loved your comment. Thank you