PHP + Jquery 实现异步传输验证

来源:互联网 发布:电视直播软件 知乎 编辑:程序博客网 时间:2024/06/15 18:17

这里我参考网上的案例,随便实现了一个一步传输验证的小功能。

1.php

<!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>PHP+Ajax 异步通讯注册验证</title>
<script type="text/javascript" src="jquery-1.7.1.js"></script> <!--千万别忘记引用jQuery文件,否则无法执行-->
<script type="text/javascript">

$("#user").blur(function(){ //文本框鼠标焦点消失事件
$.get(
  "check_user.php?user="+$("#user").val(),
  null,
  function(data) //此处get方式 可换为post方式按需求调整,其他无需修改使用方式一样
 {
  $("#chk").html(data); //向ID为chk的元素内添加html代码
 });
});
</script>

<script type="text/javascript">
        $(function(){
            $("#user").blur( function () {
                $.get(
  "check_user.php?user="+$("#user").val(),
  null,
  function(data) //此处get方式 可换为post方式按需求调整,其他无需修改使用方式一样
 {
  $("#chk").html(data); //向ID为chk的元素内添加html代码
 });
            });
        });
</script>
</head>
<body>
<form id="form" action="" method="post">
用户名:<input id="user" type="text" /> <span id="chk"></span>
</form>
</body>
</html>


验证页面

check_user.php

<?php

header("Content-type:text/html;charset=utf-8");
//GET方式获取数据(取决于异步提交时提交方式)
if($_GET['user'])
{
$user=$_GET['user'];
//此处可进行数据库匹配,本次省略直接判断
if($user=="admin")
echo "<font color=red>用户名已被注册!</font>";
else
echo "<font color=red>用户名可以使用</font>";
}else{}

?>

具体可参考如下连接:

http://www.codesky.net/article/201202/163214.html



0 0
原创粉丝点击