Ajax登录响应错误

问题描述:

我正在使用ajax和php登录,在这种情况下,我实际上正在登录succesfuly。但我试图在登录时发出警报并刷新页面。 当我尝试登录时,它给我提供了错误并且没有提醒。但是,如果我刷新页面,我看到我有PHP会话。我不明白为什么。Ajax登录响应错误

这是我的代码;

<script> 

$(document).ready(function(){ 
    $('#login_btn').click(function(){ 
     var email = $('#email').val(); 
     var password = $('#password').val(); 


     if(email == '' || password == ''){ 
     $("#login_error").html("*** Please enter your email/password"); 
     }else{    
      $('#login_error').html("<strong class='text-success'>Validating...</strong>"); 

       $.ajax({ 
       url: "login.php", 
       method: "post", 
       data:{email:email, password:password}, 
       success: function(data){ 
       if (data === 'yes') { 
        window.location.reload(); 
       }else{ 
        $('#login_error').html("<strong class='text-danger'>ERROR...</strong>"); 
       } 
      } 
       }); 

     } 
    }); 
}); 
</script> 

登录PHP:

<?php 
session_start(); 
include ('../config/setup.php'); #database connection 


if(isset($_POST['email'])){ 
$q = "SELECT * FROM users WHERE email = '$_POST[email]' AND password = '$_POST[password]'"; 
$r = mysqli_query($dbc, $q); 

if(mysqli_num_rows($r) > 0){ 
    $_SESSION['email'] = $_POST['email']; 
    echo "yes"; 
}else{ 
    echo "no"; 
} 
} 
?> 

HTML:(一模内使用的登录表单)

<div class="modal-body"> 

<div class="form-horizontal"> 
    <div class="form-group"> 
    <label for="email" class="col-sm-4 control-label">Email</label> 
    <div class="col-sm-8"> 
     <input type="email" class="form-control" name="email" id="email" placeholder="Account Email"> 
    </div> 
    </div> 
    <div class="form-group"> 
    <label for="password" class="col-sm-4 control-label">Password</label> 
    <div class="col-sm-8"> 
     <input type="password" class="form-control" name="password" id="password" placeholder="Account Password" > 
    </div> 
    </div> 

    <div class="form-group"> 
    <div class="col-sm-1"></div> 
    <div align="center" class="col-sm-10"> 
     <button name="login_btn" id="login_btn" class="btn btn-success btn-block text-center"><span id="loader_before" class="glyphicon glyphicon-log-in" aria-hidden="true"></span><i id="loader" class="fa fa-spinner fa-spin fa-x fa-fw"></i> Log in to Account</button> 
     </div> 
     <div class="col-sm-1"></div>  
    </div> 
</div> 

<div align="center" class="container-fluid"> 
<h6><a href="#"><strong class="text-danger">Forgot your password? Click here..</strong></a></h6> 
</div> 
<h5><div id="login_error" class="text-warning"></div></h5> 
</div> 

你只需要更新您如果块响应如下 `

if (data === 'yes') { 
    alert("You message here!"); 
    window.location.reload(); 
}else{ 
    $('#login_error').html("<strong class='text-danger'>ERROR...</strong>"); 
} 

`

+0

我试过了你的代码,没有给我任何提醒。只有错误... –

+0

你能分享这个错误吗?它会帮助我解决问题。你也可以试试下面的sql语句。 $ q =“SELECT * FROM users WHERE email ='”。$ _ POST ['email']。“'AND password ='”。$ _ POST ['password']。“'”; –

+0

错误是:我有一个div,id是登录错误。在JavaScript中,我使用这个div来显示记录尝试的情况。当我尝试登录时,它会给我$('#login_error')。html(“ ERROR ...”); 而不是刷新页面(window.location.reload();在Ajax)我刷新页面,我看到我已经正确登录。我需要看到我的成功股利,并刷新页面后,如果我登录正确。 –

当你在php上调用echo时,它会写入该值,但不会返回它。那就是问题所在。 您不会从login.php返回“yes”或“no”,您只是在执行echo,它只会写入该值,但不会将其返回给调用者。 ajax调用正在等待响应以'成功'回调进行处理。由于login.php没有返回任何东西,它总是会落入else子句中。 解决的办法是在login.php上更改它

if(mysqli_num_rows($r) > 0){ 
$_SESSION['email'] = $_POST['email']; 
    return "yes"; 
}else{ 
    return "no"; 
} 
+0

我已经改变回声返回;但其相同。旁;如果它的回声:在mozilla开发工具xhr答案是肯定的。如果我改变回声返回;答案为空。 –

+0

但有趣的是,它的登录。没有自动刷新,我刷新页面和会话的工作 –

+0

为什么不使用mozilla开发工具进行调试,并试图明白为什么如果hxr回答是的,你没有收到该数据成功回调 – fejanto