jQuery中的远程验证无法验证

时间:2012-02-09 16:16:28

标签: ajax json validation jquery-mobile

此表单会将电子邮件和密码插入我的MYSQL数据库

首先,我正在尝试远程验证电子邮件,以确保它不存在,还有 Pswd和Pswd2相等。

脚本写入数据库,但验证无效。我是JQ和Ajax的新手,所以任何帮助都会受到赞赏。 感谢。

表格:

    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.css" />
    <script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.js"></script>
    <script src="jquery.validate.js" type="text/javascript"></script>
    <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/additional-methods.min.js"></script>

   ... 
        <div data-role="content">
             <form  id="AddUser" class="ui-body ui-body-a ui-corner-all" data-ajax="true" >
                    <fieldset>
                        <div data-role="fieldcontain">
                            <label for="Email">Email Address:</label>
                            <input id="Email" type="email" />
                        <label for="Pswd">Password:</label>
                            <input id="Pswd" type="password" /> 
                        <label for="Pswd2">Confirm Password:</label>
                            <input id="Pswd2" type="password" />    
                        </div>
                        <button type="submit" id="submit" data-theme="b" name="submit" value="submit-value">Submit</button>
                    </fieldset>
        </form>     
        </div>

...
下面的

是文档中的js:

 <script>
    $(document).ready(function() { 

        var validator = 
        $('#AddUser').validate({ 
            rules: { 
                Pswd: { 
                    required: true, 
                }, 
                Pswd2: { 
                    required: true, 
                    equalTo: "#Pswd" 
                }, 
                Email: { 
                   required: true, 
                   email: true, 
                   remote: "process/ValidateEmail.php" 
                }
            }, 
            messages: { 
                Pswd: { 
                    required: "Provide a password", 
                }, 
                Pswd2: { 
                    required: "Repeat your password", 
                    equalTo: "Enter the same password as above" 
                }, 
                Email: { 
                    required: "Not A Valid Email Address", 
                    remote: "already in use"
                }
             }
        }); // end var validator

            if (validator)
            {
                $("#AddUser").submit(function(){        
                //make Variables
                var emailVar =$('input[id=Email]').val();
                var pswdVar = $('input[id=Pswd]').val();
                var pswdconfirm = $('input[id=Pswd2]').val();
                var querystring = "Email="+emailVar+"&Pswd="+pswdVar;

                $.post("process/AddUser.php",querystring);

            }); //end submit()
            }; // end if validator


    }); //end ready()

    </script>

Validator文件(ValidateEmail.php)

<?php 

    $UserEmail = $_POST["Email"];

    include "connection.php";

    $sqlEmail= mysql_query("Select EmailAddress from USERS where EmailAddress='$UserEmail'"); 
    $EmailCheck=mysql_fetch_array($sqlEmail);

    if (mysql_num_rows($EmailCheck) > 0) {
        echo json_encode(true);
    } else {
        echo json_encode(false);
    }

?>

2 个答案:

答案 0 :(得分:0)

经过一些研究后我回到了代码中,发现它在js和php验证中都有错误:

<script>
    $(document).ready(function()
  {

    var validator = $("#AddUser").validate({
        rules: {
            Email: {
                required: true,
                email: true,
                remote: "process/ValidateEmail.php"
            },
            Pswd: {
                required: true,
            },
            Pswd2: {
                required: true,
                equalTo: "#Pswd"
            },
        },
        messages: {
            Pswd: {
                required: "Provide a password",
            },
            Pswd2: {
                required: "Repeat your password",
                equalTo: "Password Mismatch"
            },
            Email: {
                required: "Invalid Email",
                remote: jQuery.format("{0} is already used")
            }
        },
        // the errorPlacement has to take the table layout into account
        errorPlacement: function(error, element) {

                error.appendTo(element.parent().next());
        },
        // specifying a submitHandler prevents the default submit, good for the demo
        submitHandler: function() {         

            //make Variables
            var emailVar =$('input[id=Email]').val();
            var pswdVar = $('input[id=Pswd]').val();
            var pswdconfirm = $('input[id=Pswd2]').val();
            var querystring = "Email="+emailVar+"&Pswd="+pswdVar;

            $.post("process/AddUser.php",querystring);

            $('#AddUser').clearForm();
        return false;

        },
        // set this class to error-labels to indicate valid fields
        success: 

            function(label) {
            label.html("&nbsp;").addClass("checked");

        }
        });
    }); 
</script>

答案 1 :(得分:0)

            email: { 
            required: true, 
            email: true, 
            remote: {
                url: "check-email.php",
                type: "post",
                data: {
                    email: function() {
                        return $("[name='email']").val();
                    }
                }
            }
          },

并且php必须如下所示:

$email= $_POST['email'];
include 'connect.php';
$email = trim(mysql_real_escape_string($email));
$query = mysql_query("SELECT COUNT(id) FROM users WHERE email = '$email'") or die(mysql_error());
echo (mysql_result($query, 0) == 1) ? 'false' : 'true';

小心不要只是错误或真实地回应它必须像'false'或true

那样回应