php+ajax 註冊驗證用戶名是否存在實例

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” οnblur=”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();//關閉數據庫連接
?>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章