使用jQuery验证对多个字段进行远程验证

时间:2017-12-04 15:16:27

标签: php jquery validation

我正在使用jQuery验证插件来检查用户是否在我的SQL数据库中存在问题。我想检查checkCredentials.php中的凭据。我的脚本 检查是否填写了电子邮件或密码,但检查我的数据库中是否存在凭据。谁能告诉我我做错了什么?提前致谢。

这是我的表单

            <form action="php/login.php" method="post" id="loginForm" class="form-horizontal">
                <div class="form-group">
                    <input type="email" class="form-control form-control-lg" id="email" name="email" placeholder="Email" />
                </div>
                <div class="form-group">
                    <input type="password" class="form-control form-control-lg" id="password" name="password" placeholder="Wachtwoord" />
                </div>
                <button class="btn btn-block" type="submit" name="login" value="Login">Login</button>
            </form>

这是我的脚本

    $( "#loginForm" ).validate( {
    rules: {
        email: {
            required: true,
            email: true
        },
        password: {
            required: true,
            remote: {
                url: "php/checkCredentials.php",
                type: "post",
                data: {
                    email: function () {
                        return $("#email").val();
                    }
                }
            }
        },
    },
    messages: {
        email: {
            required: "Vul alsjeblieft een e-mail adres in"
        },
        password: {
            required: "Vul alsjeblieft een wachtwoord in",
            remote: "E-mail of wachtwoord onjuist"
        },
    },

这是我的PHP文件 checkCredentials .php

<?php
require 'database-connection.php';

$email = mysqli_real_escape_string($_REQUEST['email']);
$password = mysqli_real_escape_string($_REQUEST['password']);

$query = "SELECT user_email, user_password FROM table_users WHERE user_email = '$email' AND user_password = '$password'";
$result = mysqli_query($db, $query);
if (mysqli_num_rows($result) == 0)
{
    echo 'true';
}
else
{
    echo 'false';
}
?>

1 个答案:

答案 0 :(得分:0)

我将代码更改为此代码,现在正在运行:

<强>形式:

    <form action="php/login.php" method="post" id="loginForm" class="form-horizontal">
                    <div class="form-group">
                        <input type="email" class="form-control form-control-lg" id="email" name="email" placeholder="Email" />
                    </div>
                    <div class="form-group">
                        <input type="password" class="form-control form-control-lg" id="password" name="password" placeholder="Wachtwoord" />
                    </div>
                    <button class="btn btn-block" type="submit" name="login" value="Login">Login</button>
                </form>

<强>脚本:     

    $( "#loginForm" ).validate( {
        rules: {
            email: {
                required: true,
                email: true
            },
            password: {
                required: true,
                remote: {
                    url: "php/checkCredentials.php",
                    type: "post",
                    data: {
                        email: function () {
                            return $("#email").val();
                        }
                    }
                }
            },
        },
        messages: {
            email: {
                required: "Vul alsjeblieft een e-mail adres in"
            },
            password: {
                required: "Vul alsjeblieft een wachtwoord in",
                remote: "E-mail of wachtwoord onjuist"
            },
        },
        errorElement: "em",
        errorPlacement: function ( error, element ) {
            error.addClass( "help-block" );


            element.parents( ".col-sm-5" ).addClass( "has-feedback" );

            if ( element.prop( "type" ) === "checkbox" ) {
                error.insertAfter( element.parent( "label" ) );
            } else {
                error.insertAfter( element );
            }

            if ( !element.next( "span" )[ 0 ] ) {
                $( "<span class='glyphicon glyphicon-remove form-control-feedback'></span>" ).insertAfter( element );
            }
        },
        success: function ( label, element ) {
            if ( !$( element ).next( "span" )[ 0 ] ) {
                $( "<span class='glyphicon glyphicon-ok form-control-feedback'></span>" ).insertAfter( $( element ) );
            }
        },
        highlight: function ( element, errorClass, validClass ) {
            $( element ).parents( ".col-sm-5" ).addClass( "has-error" ).removeClass( "has-success" );
            $( element ).next( "span" ).addClass( "glyphicon-remove" ).removeClass( "glyphicon-ok" );
        },
        unhighlight: function ( element, errorClass, validClass ) {
            $( element ).parents( ".col-sm-5" ).addClass( "has-success" ).removeClass( "has-error" );
            $( element ).next( "span" ).addClass( "glyphicon-ok" ).removeClass( "glyphicon-remove" );
        }
    } );

</script>

<强> checkCredentials.php:

<?php
require 'database-connection.php';

$email = mysqli_real_escape_string($db, $_POST['email']);
$password = mysqli_real_escape_string($db, $_POST['password']);

$query = "SELECT user_email, user_password FROM table_users WHERE user_email = '$email' AND user_password = '$password'";
$result = mysqli_query($db, $query);
if (mysqli_num_rows($result) > 0)
{
    echo 'true';
}
else
{
    echo 'false';
}
?>