weixin_33728708 2017-11-25 13:08 采纳率: 0%
浏览 16

Ajax登录响应错误

i'm traying to login with ajax and php, in that situation i'm logging succesfuly actually. 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. 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  
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)

<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>  
  • 写回答

2条回答 默认 最新

  • weixin_33711641 2017-11-25 13:13
    关注

    You just need to update your if block in response as below `

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

    `

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog