检查密码是否错误PHP和ajax

时间:2016-11-05 03:25:46

标签: php ajax

我试图检查登录表单中的密码是否有问题,但电子邮件确实显示在数据库中。 这是我的ajax / js代码:

$.post('li-check-email.php', {'liEmail' : $liEmail, 'liPassword' : $liPassword}, function(liData) {
        if (liData.trim() == 'existspwno') 
        {
            liValidForm = false;
            $logInSubmit.css('top', '72px');
            $liBox.css('height', '235px');
            $liBox.css('top', '36%');
            $liErrorText.text('The password entered is incorrect.');
        }
        elseif (liData.trim() == 'existsno') 
        {
            liValidForm = false;
            $logInSubmit.css('top', '72px');
            $liBox.css('height', '235px');
            $liBox.css('top', '36%');
            $liErrorText.text('The email entered is not currently registered.');
        };
    });

这是我的PHP代码:

<?php include("dbconnect.php") ?>
<?php
    $liEmail = mysqli_real_escape_string ($_POST['liEmail'])
    $liPassword = mysqli_real_escape_string ($_POST['liPassword'])
    $sqlEmail = "SELECT email FROM users WHERE email = '$liEmail'";
    $sqlPassword = "SELECT * FROM users WHERE email = '$liEmail' AND password = '$liPassword'";
    $selectEmail = mysqli_query($connection, $sqlEmail);
    $selectPassword = mysqli_query($connection, $sqlPassword);

    if (mysqli_num_rows($selectPassword) = 0) 
    {
      echo "existspwno";
    }
    else if (mysqli_num_rows($selectEmail) = 0)
    {
        echo "existsno";
    }
?>

我在数据库中输入了一封电子邮件,密码不正确,但是没有显示js错误消息。

1 个答案:

答案 0 :(得分:0)

试试这个,这是我的Jquery Ajax代码

$("#login").click(function()
        {
            var username = $("#username").val();
            var password = $("#password").val();

            $("#login").html("Authenticating...").attr("disabled", true);

            if(username == "")
                {
                    $("#username").addClass("error").focus();
                    $("#login").html("Start Managing").attr("disabled", false);
                    $("#error-message").html("Username Cannot Be Left Blank...").addClass("error-message");
                }
            if(password == "")
                {
                    $("#password").addClass("error").focus();
                    $("#login").html("Start Managing").attr("disabled", false);
                    $("#error-message").html("Password Cannot Be Left Blank...").addClass("error-message");
                }
            if(username == "" && password == "")
                {
                    $("#username").addClass("error").focus();
                    $("#password").addClass("error");
                    $("#login").html("Start Managing").attr("disabled", false);
                    $("#error-message").html("Please Enter Username & Password...").addClass("error-message");
                }

            if(username != "" && password != "")
                {
                    $.ajax({
                            url: 'check.php',
                            type: 'POST',
                            dataType: 'html',
                            data: {username: username, password: password},
                        })
                        .done(function(resp)
                            {
                                if(resp == 0)
                                    {
                                        $("#password").addClass("error").focus();
                                        $("#error-message").html("Wrong Password For Username : "+username).addClass("warning-message");
                                        $("#login").html("Start Managing").attr("disabled", false);
                                    }
                                else if(resp == "other")
                                    {
                                        $("#error-message").html("Please Contact The Admin!").addClass("error-message");
                                        $("#login").html("Start Managing").attr("disabled", false);
                                    }
                                else if(resp == 1)
                                    {
                                        $("#username").addClass("success");
                                        $("#password").addClass("success");
                                        $("#error-message").text("Redirecting To Dashboard Now..").addClass("success-flash");
                                        $("#login").html("Authorized Successfully...").attr("disabled", true);
                                        location.replace("dashboard.php");
                                    }
                                else
                                    {
                                        console.log(resp);
                                    }   
                            })
                        .fail(function()
                            {
                                console.log("error");
                            });
                }
        });

在PHP方面,这是我的代码:

$query = "SELECT password FROM site_handlers WHERE username = '".$username."' OR email = '".$username."' OR mobile = '".$username."' AND password = '".$password."' ";
                    $connection = $this->establish_connection();
                    $result = $connection->query($query);
                    $connection->close();
                    if($result->num_rows > 0)
                        {
                            $pass = $result->fetch_object()->password;

                            if($password === $pass)
                                {
                                    return 1;
                                }
                            else
                                {
                                    return 0;
                                }
                        }
                    else
                        {
                            return "other";
                        }

这是该代码的工作example

登录详情

用户名:demoadmin

密码:密码