php ajax使用者註冊檢測代碼_PHP教程

來源:互聯網
上載者:User
實只要簡單的實現ajax的檢測使用者名稱,正規點要分三個檔案。我這裡簡單點:

第一個:index.php




無標題文檔








用 戶 名*




第二個要用到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
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

$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");
?>

http://www.bkjia.com/PHPjc/371914.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/371914.htmlTechArticle實只要簡單的實現ajax的檢測使用者名稱,正規點要分三個檔案。我這裡簡單點: 第一個:index. php !DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//E...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.