试图将成功与否从ajax传递到html登录表单

时间:2016-11-22 18:24:20

标签: javascript php jquery ajax

嗨我正在尝试传递登录表单,但它一直给我成功,如果它的数据库中有错误的用户或用户,这是我的代码=>

这是ajax代码=>

$J(document).ready(function () {
    $J("#add_err").css('display', 'none', 'important');
    $J("#login").click(function () {
        email = $J("#email").val();
        password = $J("#password").val();  
        $J.ajax({ 
            type: "POST",
            url: "login.php",
            data: "email=" + email + "&password=" + password,
            success:function (data) {
                if (data) {
                    //$("#add_err").html("right username or password");
                    window.location = "dashboard.php";
                } else {
                    $J("#add_err").css('display', 'inline', 'important');
                    $J("#add_err").html("<img src='images/alert.png' /> <b>Wrong username or password </b>" );
                }
            },
            beforeSend: function ()
            {
                $J("#add_err").css('display', 'inline', 'important');
                $J("#add_err").html("<img src='images/spin.gif' /> Loading...")
            }
        }); 
        return false;
    });
});

这是形式=&gt;在文件login.php

<form action="" method="POST" id="loginForm">
            <div class="form-group has-feedback">
                <input type="email" name="email" id="email" class="form-control" placeholder="Email">
                <span class="glyphicon glyphicon-envelope form-control-feedback"></span>
            </div>
            <div class="form-group has-feedback">
                <input type="password"  name="password" id="password" class="form-control" placeholder="Password">
                <span class="glyphicon glyphicon-lock form-control-feedback"></span>
            </div>
            <div class="row">
                <div class="col-xs-8">
                    <div class="checkbox icheck">
                        <input type="checkbox" name="iCheck"  /> 
                        <b>Remember me </b>
                        <script>
                            $J(document).ready(function () {
                                $J('input').iCheck({
                                    checkboxClass: 'icheckbox_flat-blue',
                                    radioClass: 'iradio_flat',
                                    increaseArea: '-10%', // optional
                                });
                            });
                        </script>
                    </div>
                </div>
                <!-- /.col -->
                <div class="col-xs-4">
                    <button type="submit" id="login" class="btn btn-primary btn-block btn-flat">Sign In</button>
                </div>
                <!-- /.col -->
            </div>
        </form>

这是我移动到数据库的帖子=&gt;在同一个文件login.php

if (isset($_POST['email']) && isset($_POST['password'])) {
session_start();
$dbConnect = new dbconnect();
$email = $_POST['email'];
$password = $_POST['password'];
$userData = $dbConnect->verify_login($email, $password);
if (!$userData) {
    echo "false";
}}

varifylogin function =&gt;

    public function verify_login($email, $password) {
    $this->Connect();
    $sql = "SELECT email,password FROM `users_details` WHERE `email`='$email' AND `password`='$password' ";
    if ($this->res = mysqli_query($this->ind_connect, $sql)) {
        $num_row = mysqli_num_rows($this->res);
        $row = mysqli_fetch_assoc($this->res);
        if ($num_row == 1) {
            echo 'true';
            $_SESSION['email'] = $row['email'];
            $_SESSION['password'] = $row['password'];
        }
    } else {
        echo 'errr';
    }
    $this->Disconnect();
}

我真的不知道该怎么办 现在欢迎帮助Ty。

1 个答案:

答案 0 :(得分:0)

在函数中:verify_login

您必须返回布尔类型的真值,而不是文本。

然后按如下方式改进代码。

$userData = $dbConnect->verify_login($email, $password);
if ($userData) {
    echo "true";
}else{
    echo "false";
}
}

在ajax电话中

success:function (data) {
if (data=='true') {
//..
}else{
//..
}
}

另一种方法是让php返回一个json,而在ajax中它表示dataType:&#34; json&#34;

$.ajax({
    url : "",
    type : "POST",
    dataType : 'json',
    success : function(data) {
        console.log(data);
        if(data.success==true){

        }else{
        }
   }
 });

在文件php中:

echo json_encode(array('success'=>true));