从远程文件中进行Jquery用户名验证

时间:2012-02-25 08:29:45

标签: jquery ajax validation username

这是我的jquery代码:

$(document).ready(function(){
        $("#reg").validate({
            debug: false,
            rules: {
                username: {
                    required: true,
                    remote: "username_check.php"
            .............

这是我的PHP代码:

if(isset($_POST['username'])){

$username = ($_POST['username']);

if (!empty($username)) {
    $username_query=mysql_query("SELECT COUNT(`user_name`) FROM `std_info` WHERE `user_name`='$username'");
    $username_result=mysql_result($username_query, 0);

if ($username_result == 0) {
//echo 'AVAILABLE!';
$check= false;

}
else if ($username_result == 1) {
//echo 'ALREADY TAKEN.';
$check= true;

}
echo json_encode($check);

}}

但我无法回报价值。 有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

只需更改您的代码

 if (!empty($username)) {
        $username_query=mysql_query("SELECT COUNT(`user_name`) As count FROM `std_info` WHERE `user_name`='$username'");
        $username_result=mysql_result($username_query, 0);

    $row=mysql_fetch_assoc($username_result);

    $cnt=$row['count'];


  if ($cnt==1) {
    $check= true;
    }

    else {
    $check= false;
    }
    echo json_encode($check);