php ajax使用者註冊檢測代碼

來源:互聯網
上載者:User

實只要簡單的實現ajax的檢測使用者名稱,正規點要分三個檔案。我這裡簡單點:

第一個:index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.hake.cc/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.hake.cc/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
<script language="javascript" src=http://www.phpzy.com/PHPjichu/"ajax.js"></script>
</head>

<body>
<table align="center">
<tr>
     <td width="25%" class="altbg1"> 用 戶 名<font color="red">*</font>
    <input size="25" name="username" id="username" type="text" value="" onblur="startRequest(document.getElementById('username').value);" />       <br /></td>
              <td></td>
                          <td id="ckuser"></td>
</tr>
</table>
</body>
</html>
第二個要用到js:ajax.js
[php]

var xmlHttp;
function createXMLHttpRequest()
{
      if(window.XMLHttpRequest)
{
      xmlHttp = new XMLHttpRequest();//mozilla瀏覽器
}
else if(window.ActiveXObject)
{
try
{
     xmlHttp = new ActiveX0bject("Msxml2.XMLHTTP");//IE老版本
}
catch(e)
{}
try
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");//IE新版本
}
catch(e)
{}
if(!xmlHttp)
{
window.alert("不能建立XMLHttpRequest對象執行個體");
return false;
}
}
}


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

xmlHttp.open("GET","ckuser.php?name="+username,true);
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.send(null);
}


function handleStateChange()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
//alert("來自伺服器的響應:" + xmlHttp.responseText);
if(xmlHttp.responseText == "true"){
document.getElementById("ckuser").innerHTML = '此使用者名稱以被人註冊';
}
else if(xmlHttp.responseText == "false")
{
document.getElementById("ckuser").innerHTML = '檢測通過';
}
}
}
}

[/php]

第三個檔案就是php檔案:ckuser.php
<?php
        require_once("conn.php");
   $username = $_GET["name"];
        $query="select id from user where username='".$username."';";
        $res=mysql_query($query);
                if(mysql_num_rows($res)!=0)
                {
             echo "true";
                }else
                        {
                           echo "false";
                        }


?>
最後一個是資料庫連結檔案conn.php

<?php
     $conn=mysql_connect("localhost","root","l1314520") or die("資料庫伺服器串連錯誤".mysql_error());
     mysql_select_db("test",$conn) or die("資料庫訪問錯誤".mysql_error());
   mysql_query("set character set gb2312");
     mysql_query("set names gb2312");
?>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.