简体   繁体   English

jQuery远程验证未提交表单

[英]jquery remote validation is not submitting the form

I cannot get this basic form validation to submit. 我无法提交此基本表单验证。 I read that the remote is the cause. 我读到遥控器是原因。 Is there any way to fix it as I need it to check if a username exists before registering a new user? 有什么办法可以解决它,因为我需要它在注册新用户之前检查用户名是否存在?

I am using jQuery Validation Plugin v1.12.0 if it matters 如果有问题,我正在使用jQuery Validation Plugin v1.12.0

$("#regForm").validate({
   rules: {
        username: {         
            remote: {   
                url: "username_db.php", 
                type: "post"     
            }   
        }
    },
    messages: {
        username: {         
            remote: "Username already in use"
        }
    }
});

PHP: PHP:

require_once 'db.php';

if ( $conn = connect() ) {
    $row = $conn->prepare("SELECT * FROM users WHERE username = '".$_REQUEST['username']."'");
    $row->execute();
    $rs = $row->rowCount();

    if($rs > 0){
        echo 'false';
    }else{
        echo 'true';
    }
}   

EDIT: 编辑:

<form action="" method="post" id="regForm" name="regForm">
    <input type="text" name="username">
    <input type="submit" name="go" value="GO">          
</form>

A few things; 一些东西;

First: You must protect yourself against SQL injection - see for example What is SQL injection? 第一:您必须保护自己免受SQL注入的侵害-例如,参见什么是SQL注入?

Second: Your php must output the 'true' or 'false' value, not return it. 第二:您的php必须输出 “ true”或“ false”值,而不返回它。 According to the remote-method documentation, it could also output a json-encoded error message like "The user does not exist" 根据远程方法文档,它还可以输出json编码的错误消息,例如"The user does not exist"

Third: You can use the error_log() function to debug your PHP code and ensure that it's running - the messages appear in the php log file. 第三:您可以使用error_log()函数调试您的PHP代码并确保其正在运行-消息出现在php日志文件中。

So try this for your PHP code: 因此,请为您的PHP代码尝试以下操作:

require_once 'db.php';

//(Using PDO for db access - see http://php.net/manual/en/book.pdo.php )
if ($pdo = connect() ) { 
    $stmt = $pdo->prepare("SELECT * FROM users WHERE username = :username");
    $stmt->bindValue(":username", $_REQUEST['username']);
    if ( $stmt->fetchAll() ) 
        echo "true";
    else 
        echo json_encode("User does not exist");
}

Although you have not specified what the exact problem is, you are not sending back any values to the validate plugin. 尽管您没有指定确切的问题是什么,但是您没有将任何值发送回validate插件。

The plugin expects something that evaluates to valid json and although you have a return statement, you are not actually outputting anything. 该插件需要能够评估为有效json的内容,尽管您具有return语句,但实际上并未输出任何内容。

The end of your php script should be something like: 您的php脚本的结尾应类似于:

if($rs > 0){
    echo json_encode(false);    // returns "false"
}else{
    echo json_encode(true);     // returns "true"
}

Also, the reason you prepare a statement, is to protect yourself from sql injection. 另外,准备语句的原因是为了保护自己免受sql注入。 You would need a placeholder and bind the value of your variable after preparing. 准备之后,您将需要一个占位符并绑定变量的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM