php-jQuery.validator.addMethod检查用户名 - php

我是php和jQuery的新手,我已经用Google搜索了一天,但无法解决问题。我想检查用户名是否被使用,但是无论输入什么,我都会得到“用户名已被使用”。
所以这是我的代码:

$(document).ready(function(){
    $('#registration-form').validate({
    rules: {            
     username: {
            required: true,
            minlength: 3,
            maxlength: 25,
            letters: true,
            checkUsername: true,
        },        
      password: {
            required: true,
            minlength: 6,
            maxlength: 12
        },
    confirmpassword: {
            required: true,
            minlength: 6,
            maxlength: 12,
            equalTo: "#password"
        },        
      email: {
            required: true,
            email: true
        },  

     country:{
            required: true,
     }
            },

        highlight: function(element) {
            $(element).closest('.form-group').addClass('error');
            $(element).removeClass("valid");
        },
        success: function(element) {
            element
            .addClass('valid')
            .closest('.form-group').removeClass('error');
        }
  });

  $(".close, #close").click(function() {
        $("label.error").hide();
        $(".error").removeClass("error");
        $("input.valid").removeClass("valid");
    });

    $.validator.addMethod("letters", function(value, element) {
    return this.optional(element) || value == value.match(/^[a-zA-Z_]+$/);
    },"Letters and underscore only.");

    $.validator.addMethod("checkUsername", function(value, element){
        var response='';
        $.ajax({
            type: "POST",
            url: 'checkname.php',
            data:"username="+value,
            async:false,
            success:function(data){
            response = data;
            }
                });
            if(response == 0)
            {
                return true;
            }
            else
            {
            return false;
            }

    }, "Username is Already Taken");

}); 

这是我的checkname.php

<?php
include 'connect.php';

 if (isSet($_POST['username'])) {                                                               
  $username = mysql_real_escape_string($_POST['username']);                                  
  $check_for_username = mysql_query("SELECT * FROM user WHERE username='$username'"); 
if (mysql_num_rows($check_for_username)>0) {
    echo "false";                                                                           
} else {
    echo "true";                                                                          
}
}


?>

请帮助我,我很沮丧.....

参考方案

您应该使用the remote method,它已经为此特定目的进行了测试和证明。没有理由重新发明已经完成的事情。

rules: {            
    username: {
        required: true,
        minlength: 3,
        maxlength: 25,
        letters: true,
        remote: {  // value of 'username' field is sent by default
            type: 'POST',
            url: 'checkname.php'
        }
    }
    ....

您的checkname.php

include 'connect.php';

if (isSet($_POST['username']))
{                                                               
    $username = mysql_real_escape_string($_POST['username']);

    $check_for_username = mysql_query("SELECT * FROM user WHERE username='$username'"); 

    if (mysql_num_rows($check_for_username) > 0)
    {
        echo json_encode("Username is Already Taken");   
        // you could even try something like this:
        // echo json_encode($username . " is Already Taken");                                                                        
    } 
    else 
    {
        echo "true";                                                                          
    }
}

注意:mysql_在PHP中已被弃用,应替换为mysqlipdo_mysql。参见:php.net/manual/en/function.mysql-query.php

另请参见:jQuery Validate remote method usage to check if username already exists

jQuery Ajax加载仅适用于单个单词变量 - php

在我的PHP文件中,我将一些变量从输入框传递到链接中,该链接通过jQuery的ajax load函数在其URL中使用该变量加载页面。整个系统运行良好,但仅适用于单字变量。每当涉及到空格时,我的Ajax通话都会中断。我假设这是一个编码问题,但是我也有一些疑问。基本上,它归结为我的PHP文件中的这一行代码,这导致了混乱。是否有可能找出格式是否正确,或者为什么在多…

jQuery .ajax将整个JavaScript数组传递给PHP - php

tdata = new Array(); tdata['id'] = "helloid"; tdata['name'] = "helloname"; tdata['location'] = "hellolocation"; tdata[&#…

使用PHP变量将多个参数传递给AJAX JQuery - php

我试图将2个值传递给AJAX JQuery函数。我的函数是由下拉列表的onChange触发的,该列表从数据库获取数据并通过while循环运行,并使用PHP输出,如下所示:$dropDown = "<label for='drop_down_list'>Drop Down: </label> <sel…

jQuery-找不到Ajax网址 - php

好的,我敢肯定这确实很容易,而且我很愚蠢,但是似乎并不能深入了解它。我试图从我的js文件“ custom.js”中对“ helpers.php”中的某些代码进行简单的AJAX调用。但是,我仍然收到404错误,因为我似乎并没有正确遍历文件夹,尽管我确信我正在...我的文件夹结构如下:html index.php js/ custom.js includes h…

jQuery AJAX不向PHP发送多个提交按钮 - php

所以我的问题是,当我将表单提交到PHP时,使用jQuery通过AJAX提交表单时,使用jQuery AJAX时哪个按钮提交表单并不重要,但是当使用表单操作时,表单会提交并让我知道删除还是删除按下了更新按钮。例如,这是提交表单的HTML<input type="submit" name="update" value…