一、前端代码
<body>
用户名:<input type="text" id="user" /><br/>
密码:<input type="text" id="pass" /><br/>
<input type="button" value="登录" id="login" />
<span></span>
<script src="ajax.js"></script>
<script>
function Login(){
this.user = document.getElementById("user");
this.pass = document.getElementById("pass");
this.login = document.getElementById("login");
this.span = document.querySelector("span");
this.url = "http://localhost/ajxa/data/login.php";
this.init();
}
Login.prototype.init = function(){
var _this = this;
this.login.onclick = function(){
ajaxGet(_this.url,function(res){
_this.res = JSON.parse(res);
_this.display();
},{
user:_this.user.value,
pass:_this.pass.value
});
}
}
Login.prototype.display = function(){
switch(this.res.status){
case 0:
this.span.innerHTML = "登录成功";
break;
case 1:
this.span.innerHTML = this.res.msg;
break;
case 2:
this.span.innerHTML = this.res.msg + "<a href='http://www.baidu.com' >请注册</a>";
}
}
new Login();
</script>
</body>
二、后端php代码
$u = "admin";
$p = "123";
$user = @$_REQUEST["user"];
$pass = @$_REQUEST["pass"];
if($u == $user && $p == $pass){
echo '{"msg":"成功","status":0,"userMsg":{}}';
}else if($u == $user && $p != $pass){
echo '{"msg":"密码错误","status":1,"userMsg":{}}';
}else{
echo '{"msg":"该用户不存在","status":2,"userMsg":{}}';
}