JQuery远程验证不显示错误

时间:2015-12-30 10:56:12

标签: php jquery jquery-validate

我使用的是JQuery Validation插件,除了用户名上的远程验证之外,它还是独一无二的。

脚本返回“true”或“false”,您将从以下代码中看到。但我不确定为什么它没有显示错误...值得一提的是显示用户的其他错误。

Jquery验证规则:

User: {
                required: true,
                minlength: 6,
                remote:{
                    url: 'scripts/userCheck.php',
                    type: "post"
                }
            }

JQuery验证消息:

User: {
                    required: "Please Enter a Username",
                    minlength: "Username must be more than 6 characters in Length",
                    remote: "User already exists"
                }

userCheck.php:

<?php
/**
 * Created by PhpStorm.
 * User: nathanenglish5
 * Date: 30/12/2015
 * Time: 09:05
 */
include_once "init.php";
include_once "../resources/signup.class.php";
$signup = new signup($DB_con);

$return = "false";
$count = 0;

if (isset($_Post['User'])) {
    $uid = $_Post['User'];
    $sql = "SELECT COUNT(username) FROM username WHERE username = '$uid'";
    $count = $signup->dataview($sql);

    if($signup->dataview($sql)==0){
        $return = "true";
    }
}
?>

所有dataview类都返回一个数字。

任何帮助或指导都会很棒!

2 个答案:

答案 0 :(得分:0)

试试这个:

//Our validation script will go here.

    $(document).ready(function(){

        //validation implementation will go here.
        $("#form_id").validate({
            rules: {

                user: {
                    required: true,
                   minlength: 6,
                },
               remote: {
                  url: 'scripts/userCheck.php',
                type: "post"
               }
           },
           messages: {
               user: {
                    required: "Please Enter a Username",
                minlength: "Username must be more than 6 characters in Length",

               },
               remote: {
                   remote: "User already exists"

               }
           }
       });
 })

答案 1 :(得分:0)

我阅读了其他几个论坛,发现我需要回显结果并将userCheck.php更改为以下内容:

<?php
/**
 * Created by PhpStorm.
 * User: nathanenglish5
 * Date: 30/12/2015
 * Time: 09:05
 */
include_once "init.php";
include_once "../resources/signup.class.php";
$signup = new signup($DB_con);

$return = "false";
$count = 0;

if (isset($_POST['User'])) {
    $uid = $_POST['User'];
    $sql = "SELECT COUNT(username) FROM username WHERE username = '$uid'";
    $count = $signup->dataview($sql);

    if($signup->dataview($sql)==0){
        $return = "true";
    }
}
echo $return;
?>