php第二十一节课
AJAX
<!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" /><span id="xinxi"></span></div>
</body>
<script type="text/javascript">
$(document).ready(function(e) {
$("#uid").blur(function(){
//把文本框的值取出来
var uid = $(this).val();
//调用ajax
$.ajax({
url:"ChuLi.php",//处理页面
data:{u:uid}, //传递的数据,JSON数据
type:"POST", //提交方式
datatype:"TEXT", //返回数据的类型
success:function(data){ //回调函数
if(data=="OK")
{
var str = "该用户名可以使用!";
$("#xinxi").html(str);
}
else
{
var str = "<span style='color:red'>该用户名已经存在!</span>";
$("#xinxi").html(str);
}
}
});
})
});
</script>
</html>
<!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>
</body>
<script type="text/javascript">
$(document).ready(function(e) {
$.ajax({
url:"error.php",
datatype:"TEXT",
success: function(data){
alert(data);
}
});
});
</script>
</html>
<?php
$uid = $_POST["u"];
$pwd = $_POST["p"];
include("DBDA.php");
$db = new DBDA();
$sql = "select count(*) from Users where Uid='{$uid}' and Pwd='{$pwd}'";
$attr = $db->Query($sql);
if($attr[0][0]==1)
{
echo "OK";
}
else
{
echo "NO";
}
<!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>
<h1>登录</h1>
<div>用户名:<input type="text" id="uid" /></div>
<div>密码:<input type="text" id="pwd" /></div>
<div><input type="button" id="btn" value="登录" /></div>
<div id="test" style="width:100px; height:100px; background-color:#F30"></div>
</body>
<script type="text/javascript">
$(document).ready(function(e) {
/*
数据传输:同步:传输必须等到接收方接收到,才能传下一个。异步:传输不用等到对方接收就可以继续传递。
AJAX:同步:AJAX必须处理完才能继续向下执行。异步:AJAX在处理数据的同时代码继续往下执行
*/
$("#btn").click(function(){
var uid = $("#uid").val();
var pwd = $("#pwd").val();
$.ajax({
url:"LoginChuLi.php",
data:{u:uid,p:pwd},
type:"POST",
dataType:"TEXT",
success: function(data){
if(data=="OK")
{
window.location = "test.php";
}
else
{
alert("用户名或密码错误!");
}
},
});
})
$("#test").click(function(){
$("#test").html("");
$.ajax({
async:false, //变为同步AJAX
url:"TestCL.php",
datatype:"TEXT",
success:function(data){
$("#test").html(data);
}
});
alert($("#shuju").html());
})
});
</script>
</html>
<?php
//接收传递过来的参数
$uid = $_POST["u"];
//查数据库
include("DBDA.php");
$db = new DBDA();
$sql = "select count(*) from Users where Uid='{$uid}'";
$attr = $db->Query($sql);
if($attr[0][0] == 1)
{
echo "NO";
}
else
{
echo "OK";
}
<!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>
</head>
<body>
<?php
include("DBDA.php");
$db = new DBDA();
$sql = "select * from User";
$attr = $db->Query($sql);
echo $attr[0][1];
?>
</body>
</html>