熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> PHP編程 >> 正文

php ajax用戶注冊檢測代碼

2013-11-15 12:38:11  來源: PHP編程 

  實只要簡單的實現ajax的檢測用戶名正規點要分三個文件我這裡簡單點:

  第一個indexphp
<!DOCTYPE html PUBLIC "//WC//DTD XHTML Transitional//EN" "
<html xmlns="
<head>
<meta httpequiv="ContentType" content="text/html; charset=utf" />
<title>無標題文檔</title>
<script language="javascript" src=http://wwwphpzycom/PHPjichu/"ajaxjs"></script>
</head>

  <body>
<table align="center">
<tr>
     <td width="%" class="altbg"> 用 戶 名<font color="red">*</font>
    <input size="" name="username" id="username" type="text" value="" onblur="startRequest(documentgetElementById(username)value);" />       <br /></td>
              <td></td>
                          <td id="ckuser"></td>
</tr>
</table>
</body>
</html>
第二個要用到jsajaxjs
[php]

  var xmlHttp;
function createXMLHttpRequest()
{
      if(windowXMLHttpRequest)
{
      xmlHttp = new XMLHttpRequest();//mozilla浏覽器
}
else if(windowActiveXObject)
{
try
{
     xmlHttp = new ActiveXbject("MsxmlXMLHTTP");//IE老版本
}
catch(e)
{}
try
{
xmlHttp = new ActiveXObject("MicrosoftXMLHTTP");//IE新版本
}
catch(e)
{}
if(!xmlHttp)
{
windowalert("不能創建XMLHttpRequest對象實例");
return false;
}
}
}

  
function startRequest(username)
{
createXMLHttpRequest();//特編

  xmlHttpopen("GET""ckuserphp?name="+usernametrue);
xmlHttponreadystatechange = handleStateChange;
xmlHttpsend(null);
}

  
function handleStateChange()
{
if(xmlHttpreadyState==)
{
if(xmlHttpstatus==)
{
//alert("來自服務器的響應" + xmlHttpresponseText);
if(xmlHttpresponseText == "true"){
documentgetElementById("ckuser")innerHTML = 此用戶名以被人注冊;
}
else if(xmlHttpresponseText == "false")
{
documentgetElementById("ckuser")innerHTML = 檢測通過;
}
}
}
}

  [/php]

  第三個文件就是php文件:ckuserphp
<?php
        require_once("connphp");
   $username = $_GET["name"];
        $query="select id from user where username="$username";";
        $res=mysql_query($query);
                if(mysql_num_rows($res)!=)
                {
             echo "true";
                }else
                        {
                           echo "false";
                        }

  
?>
最後一個是數據庫鏈接文件connphp

  <?php
     $conn=mysql_connect("localhost""root""l") or die("數據庫服務器連接錯誤"mysql_error());
     mysql_select_db("test"$conn) or die("數據庫訪問錯誤"mysql_error());
   mysql_query("set character set gb");
     mysql_query("set names gb");
?>


From:http://tw.wingwit.com/Article/program/PHP/201311/21329.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.