1

我正在使用 JQuery Bassistance Form Validation 插件,一切正常,直到我尝试检查 MySQL 数据库中的现有电子邮件。

这是我的代码:

    $(document).ready(function(){
    $('#signup').validate({
    rules: {
            email: {
            required: true,
        email: true,
        remote: {
            type: 'POST',
            url: "email_check.php"
        }//end remote
        }//end email
    },//end rules
    messages: {
        email: {
            remote: "Email already in use."
        }//end email
    } //end messages
    }); //end validate

    }); //end ready

这是我的 email_check.php 文件:

    <? php

    $dbc = mysqli_connect('localhost', 'root', '', 'step1_db')
    or die('Error connecting to the database');

    $email = $_GET['email'];

    $check_email = "SELECT * FROM users WHERE email = '$email'";
    $existing = mysqli_query($dbc, $check_email);
    $num_rows = $existing->num_rows;    

    if ($num_rows == 1){
    echo json_encode(FALSE);

    }else{

    echo json_encode(TRUE);
    }

    mysqli_close($dbc);

    ?>

所以现在,如果我尝试将数据库中存在的电子邮件输入到我的表单中,我会收到无效电子邮件的默认错误消息,而不是“电子邮件已在使用中”。即使我输入了数据库中不存在的有效电子邮件,错误消息也不会消失。

我猜问题出在我的 php 文件中……我试过使用“return true”和“return false”,但它们也没有用。我将我的代码更改为 echo json_encode(FALSE) 从与此类似主题的答案中。有没有人能够指出这里发生了什么?

4

2 回答 2

1

From the documentation:

The serverside resource is called via $.ajax (XMLHttpRequest) and gets a key/value pair, corresponding to the name of the validated element and its value as a GET parameter. The response is evaluated as JSON and must be true for valid elements, and can be any false, undefined or null for invalid elements, using the default message; or a string, eg. "That name is already taken, try peter123 instead" to display as the error message.

It looks then like you are responding correctly. It looks like your path to the file is wrong since you are getting a 404 error in the console. FIx the path and the rest looks good.

于 2013-05-22T00:23:01.700 回答
1

您的 php 文件将电子邮件变量设置为 GET,而远程验证功能设置为 POST,更改其中一个以匹配,您应该设置

于 2013-05-22T02:28:38.060 回答