html代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用戶名是否存在</title>
</head>
<body>
<form action="" method="post">
<table>
<tr>
<td>用戶名</td>
<td><input type="text" name="username" placeholder="請輸入用戶名" class="name" id="name" ></td>
<td><span class="note"></span></td>
</tr>
<tr>
<td>密碼</td>
<td><input type="password" name="password" class="pwd" ></td>
</tr>
<tr><td><input type="submit" id="check"></td></tr>
</table>
</form>
</body>
</html>
js代碼(當(dāng)鼠標(biāo)移開用戶名標(biāo)簽時,ajax引擎自動與后臺實現(xiàn)異步交互,從而完成驗證)
<script type="text/javascript">
var name=document.getElementById('name');
var pwd=document.getElementsByClassName('pwd')[0];
document.querySelector('.name').onblur=function () {
document.querySelector('.note').innerHTML='驗證中……';
//1.創(chuàng)建對象
var xhr=new XMLHttpRequest();
//2.設(shè)置請求行(get請求數(shù)據(jù)寫在url后面)
xhr.open('post','check.php');
//3.設(shè)置請求頭(get請求可以省略,post不發(fā)送數(shù)據(jù)也可以省略)
xhr.setRequestHeader('content-type',"application/x-www-form-urlencoded");
//3.5注冊回調(diào)函數(shù)
xhr.onload=function () {
if(xhr.status==200 && xhr.readyState==4)
{
console.log(xhr.responseText);
var data=JSON.parse(xhr.responseText);
console.log(data);
if(data.flag==3) {
document.querySelector('.note').innerHTML = data.msg;
}
}
};
//4.請求主體發(fā)送(get請求為空,或者寫null,post請求數(shù)據(jù)寫在這里,如果沒有數(shù)據(jù),直接為空或者寫null)
//post請求發(fā)送數(shù)據(jù) 寫在send中
//key=value&key2=value2
xhr.send("username="+document.getElementById('name').value);
};
</script>
后臺php文件(check_username.php):
<?php
//print_r($_POST);
$flag=0;
$msg='failure';
$username=isset($_POST['username'])?$_POST['username']:"";
$password=isset($_POST['password'])?$_POST['password']:"";
if($username==='admin'){
$flag=3;
$msg='用戶名正確';
}else {
$flag=3;
$msg='用戶名不存在';
}
?>
知識點-----AJAX - onreadystatechange 事件
當(dāng)發(fā)送一個請求后,客戶端需要確定這個請求什么時候會完成,因此,XMLHttpRequest對象提供了onreadystatechange
事件機制來捕獲請求的狀態(tài),繼而實現(xiàn)響應(yīng)。
當(dāng)請求被發(fā)送到服務(wù)器時,我們需要執(zhí)行一些基于響應(yīng)的任務(wù)。
每當(dāng)readyState
改變時,就會觸發(fā)onreadystatechange
事件。
readyState
屬性存有 XMLHttpRequest 的狀態(tài)信息。
下面是 XMLHttpRequest 對象的三個重要的屬性:
注意:POST請求不加請求頭,數(shù)據(jù)是傳不到后臺的
二.提交時完成后用戶名與密碼的驗證
創(chuàng)建一個后臺文件(check_login.php)用來驗證用戶名與密碼
新建php文件check_login.php(用戶數(shù)據(jù)這里寫死,一般是從數(shù)據(jù)庫查詢得到的)
$username=isset($_POST['username'])?$_POST['username']:"";
$password=isset($_POST['password'])?$_POST['password']:"";
if($username=='admin' && $password==123){
$flag=1;
$msg='登錄成功';
} else {
$flag=2;
$msg='密碼錯誤';
}
$response=[
'flag'=>$flag,
'msg'=>$msg,
];
echo json_encode($response);
在原來的登錄界面的js腳本里加入點擊時的驗證
document.getElementById('check').onclick=function () {
var xhr=new XMLHttpRequest();
xhr.open('post','check_login.php');
xhr.setRequestHeader('content-type',"application/x-www-form-urlencoded");
xhr.onreadystatechange=function () {
if(xhr.readyState==4 && xhr.status==200){
var data=JSON.parse(xhr.responseText);
if(data.flag==1) {
alert(data.msg);
console.log(data);
}else if(data.flag==2){
alert(data.msg);
console.log(data);
}
}
};
xhr.send('username='+document.getElementById('name').value+'&password='+pwd.value);
}
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!