用户登录验证例题用的ajax

时间:2023-03-10 07:24:06
用户登录验证例题用的ajax

1.登录页面

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script src="jquery-1.11.2.min.js"></script>
</head> <body>
<div>用户名:<input type="text" id="uid" /></div>
<div>密&nbsp;码:<input type="password" id="pwd" /></div>
<div><input type="button" value="登录" id="btn" / ></div>
</body>
<script type="text/javascript"> $("#btn").click(function(){
var u=$("#uid").val();
var p=$("#pwd").val();
$.ajax({
url:"dlchuli.php",
data:{u:u,p:p},
type:"POST",
dataType:"TEXT",
success: function(data){
if(data.trim()=="ok"){
window.location.href="main.php";
}else{
alert("用户名或密码错误");
}
}
});
});
</script>
</html>

2.处理页面

<?php
$u=$_POST['u'];
$p=$_POST['p'];
include("./lei/AAA.class.php");
$db=new AAA();
$sql="select password from yuangong where username='{$u}'";
$a=$db->StrQuery($sql);
if($p!="" && $a==$p ){
echo "ok";
}else{
echo "no";
}
?>