简体   繁体   English

Ajax登录响应错误

[英]Ajax Login Response Error

i'm traying to login with ajax and php, in that situation i'm logging succesfuly actually. 我正在用ajax和php登录,在这种情况下,我实际上在记录成功。 But i'm trying to make an alert and refresh the page when logged in. When i attempt to login, its gives me error and no refreshing. 但是我正在尝试发出警报并在登录时刷新页面。当我尝试登录时,它给了我错误并且没有刷新。 But if i refresh the page, i see i have session in php. 但是,如果我刷新页面,就会看到我在php中进行会话。 I don't understand why. 我不明白为什么。

Here is my code; 这是我的代码;

<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>

login php: 登录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: (Using login form inside a modal) 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>  

You just need to update your if block in response as below ` 您只需要更新if块即可,如下所示:

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

` `

When you call echo on php, it will write the value, but not return it. 当您在php上调用echo时,它将写入该值,但不会返回它。 That's the problem. 那就是问题所在。 You are not returning "yes" or "no" from login.php, you're just doing echo, which will only write the value but not return it to the caller. 您不是从login.php返回“是”或“否”,而是在执行echo,这只会写入值,而不会将其返回给调用者。 The ajax call is waiting for a response to process it with the 'success' callback. ajax调用正在等待响应,以使用“成功”回调对其进行处理。 Since login.php is not returning anything, it will always fall into the else clause. 由于login.php不返​​回任何内容,因此它将始终属于else子句。 The solution is to change this on login.php 解决方案是在login.php上更改此设置

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM