登录

<--HTML代码!->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body>
<label>用户名</label>
<input type="text" id="username"><br>
<label>密码</label>
<input type="password" id="password"><br>
<button value="button" id="btn">登录</button>
<div id="result"></div>
<script>
window.onload = function(){
document.getElementById('btn').onclick = function(){
var username = document.getElementById('username').value;
var password = document.getElementById('password').value;
//兼容浏览器 初始化
var xhr;
if(window.XMLHttpRequest){
xhr = new XMLHttpRequest();
}else{
xhr = new ActiveXObject("Microsoft.XMLHttP")
}
//准备好了
var url = "demo/03Login.php?username=" + username + "&password=" + password;
xhr.open("get",url);
xhr.send();
//开始监听获取到的数据
xhr.onreadystatechange = function(){
if(xhr.readyState === 4 && xhr.status === 200){
//alert(xhr.responseText);
if( xhr.responseText == 2){
document.getElementById("result").innerHTML = "登录成功";
}else if(xhr.responseText == 1){
document.getElementById("result").innerHTML = "登录名或密码错误";
}
}
};
}
}

</script>
</body>
</html>

<--php代码-->

<?php
header("Content-type: text/html; charset=utf-8");
$username = $_GET['username'];
$password = $_GET['password'];
if($username === "admin" && $password === "123"){
echo 2;

}else{
echo 1;
}

?>




posted @ 2018-03-20 17:07  寻觅聪  阅读(280)  评论(0编辑  收藏  举报