PHP语言 -- Ajax 注册用户名是否可用
<body> <h1>注册</h1> <div><input type="text" id="user" /><div></div></div> </body> <script type="text/javascript"> $(document).ready(function(e){ //判断用户名是否可用 $("#user").blur(function(){ var uid = $(this).val(); $.ajax({ url:"chuli.php", data:{u:uid,type:1}, type:"POST", datatype:"TEST" success:function(d){ if(d=="OK")
{
$("$ts").html("用户名可用");
}
else
{
$("#ts").html("<span style='color=red'>该用户名已经存在</span>");
} } }); }) }); </script>
chuli.php
<?php //不要有空 顶到头写 $type=$_POST["type"]; include "DBDA.php"; $db = new DBDA(); switch($type) { case 0: $uid = $_POST["u"]; //接收 $pwd = $_POST["p"]; $sql = "select count(*) from login where username='{$uid}' and password='{$pwd}'"; $attr = $db->Query($sql); if($attr[0][0]==0) { echo "用户名密码不正确"; } else { echo "OK"; } break; case 1: $uid = $_POST["u"]; $sql = "select count(*) from login where username='{$uid}'"; $attr = $db->Query($sql); if($attr[0][0]==0) { echo "OK"; } else { echo "NO"; } break; }