Ajax實現(xiàn)異步用戶名驗證功能
先看看布局比較簡單,效果圖如下

ajax功能:
當用戶填寫好賬號切換到密碼框的時候,使用ajax驗證賬號的可用性。檢驗的方法如下:首先創(chuàng)建XMLHTTPRequest對象,然后將需要驗證的信息(用戶名)發(fā)送到服務(wù)器端進行驗證,最后根據(jù)服務(wù)器返回狀態(tài)判斷用戶名是否可用。
function checkAccount(){
var xmlhttp;
var name = document.getElementById("account").value;
if (window.XMLHttpRequest)
xmlhttp=new XMLHttpRequest();
else
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.open("GET","login.php?account="+name,true);
xmlhttp.send();
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200)
document.getElementById("accountStatus").innerHTML=xmlhttp.responseText;
}
運行結(jié)果

代碼實現(xiàn)
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Ajax登陸驗證</title>
<script type="text/javascript">
function checkAccount(){
var xmlhttp;
var name = document.getElementById("account").value;
if (window.XMLHttpRequest)
xmlhttp=new XMLHttpRequest();
else
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.open("GET","login.php?account="+name,true);
xmlhttp.send();
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200)
document.getElementById("accountStatus").innerHTML=xmlhttp.responseText;
}
}
</script>
</head>
<body>
<div id="content">
<h2>使用Ajax實現(xiàn)異步登陸驗證</h2>
<form>
賬 號:<input type="text" id="account" autofocus required onblur="checkAccount()"></input><span id="accountStatus"></span><br><br>
密 碼:<input type="password" id="password" required></input><span id="passwordStatus"></span><br><br>
<input type="submit" value="登陸"></input>
</form>
</div>
</body>
</html>
login.php
<?php
$con = mysqli_connect("localhost","root","GDHL007","sysu");
if(!empty($_GET['account'])){
$sql1 = 'select * from login where account = "'.$_GET['account'].'"';
//數(shù)據(jù)庫操作
$result1 = mysqli_query($con,$sql1);
if(mysqli_num_rows($result1)>0)
echo '<font style="color:#00FF00;">該用戶存在</font>';
else
echo '<font style="color:#FF0000;">該用戶不存在</font>';
mysqli_close($con);
}else
echo '<font style="color:#FF0000;">用戶名不能為空</font>';
?>
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助。
相關(guān)文章
Ajax的內(nèi)部實現(xiàn)機制、原理與實踐小結(jié)
AJAX全稱為"Asynchronous JavaScript and XML"(異步JavaScript和XML),Ajax不是一個技術(shù),它實際上是幾種技術(shù),每種技術(shù)都有其獨特這處,合在一起就成了一個功能強大的新技術(shù)。2010-06-06
實現(xiàn)AJAX異步調(diào)用和局部刷新的基本步驟
AJAX?可以在不重新加載整個網(wǎng)頁的情況下,與服務(wù)器交換數(shù)據(jù),并且更新部分網(wǎng)頁,下面這篇文章主要給大家介紹了關(guān)于實現(xiàn)AJAX異步調(diào)用和局部刷新的基本步驟,需要的朋友可以參考下2022-03-03
使用ajax技術(shù)無刷新動態(tài)調(diào)用新浪股票實時數(shù)據(jù)
由于最近網(wǎng)速慢的緣故,查看股票信息時網(wǎng)頁老是打不開。這幾天一直在研究ajax,于是用jquery自己做了一個自動讀取新浪股票實時數(shù)據(jù)的頁面2014-08-08
使用getJSON()異步請求服務(wù)器返回json格式數(shù)據(jù)的實現(xiàn)
下面小編就為大家?guī)硪黄褂胓etJSON()異步請求服務(wù)器返回json格式數(shù)據(jù)的實現(xiàn)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06

