dongle7882 2016-08-01 15:08
浏览 86

使用jQuery Ajax和PHP登录过程

Hey guys I need some help.

My problem is the error div is not displaying the content that I want it to appear in the success function of my AJAX request. I tried alerting the response and it returns true if user-name and password is correct and returns false if incorrect.

I don't know why its not working.

so this is my code

<!DOCTYPE html>
<html>
<head>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

</head>
<body>

  <div class="container" name="main-div">
    <div id="error">
AA
    </div>
    <center>
      <div name="login-div">
      <h2>LOGIN PAGE</h2>
      <form method="post" class="login_form">
          Username: <input type="text" name="username"></br></br>
          Password: <input type="password" name="password"></br></br>
          <button type="submit"  name="button_login" id="button_login">LOGIN</button>
      </form>
      </div>
      </center>
  </div>

  <script src="https://code.jquery.com/jquery-3.1.0.js"></script>
  <script>
  $(document).ready(function(){
    $("#button_login").click(function(){
      var data = $(".login_form").serialize();
      $.ajax({
        type: 'POST',
        url: 'login_process.php',
        data: data,
        beforeSend: function(){
          alert(data);
        },
        success : function(response){
          //alert(response);
          if(response=="true"){
            $("#error").html("CORRECT USERNAME AND PASSWORD")
            //window.location="home.php";
        }else{
            $("#error").html('<a>WRONG</a>');
          });
          }
        }
      });
    });
  });
  </script>
</body>
</html>

Thanks in advance

  • 写回答

1条回答 默认 最新

  • dongliao9018 2016-08-01 15:52
    关注

    There is an extra closing bracket in the script what you wrote. Try to open the page in the chrome and open the developers console to see the syntax error.

    Login.html:44 Uncaught SyntaxError: Unexpected token )
    

    I corrected the syntax as below

    $(document).ready(function(){
    $("#button_login").click(function(e){
    e.preventDefault();
      var data = $(".login_form").serialize();
      $.ajax({
        type: 'POST',
        url: 'login_process.php',
        data: data,
        beforeSend: function(){
          alert(data);
        },
        success : function(response){
          //alert(response);
            if(response=="true"){
                $("#error").html("CORRECT USERNAME AND PASSWORD")
            //window.location="home.php";
            }else{
                $("#error").html('<a>WRONG</a>');
            }
        },
        error : function(response) {
            $("#error").html('error'+ error);
        }
    
      });
    });
    

    });

    评论

报告相同问题?

悬赏问题

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