简体   繁体   English

jQuery Ajax和PHP的登录过程

[英]Login Process with jQuery Ajax and 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. 我的问题是错误div未显示我希望它显示在我的AJAX请求的成功函数中的内容。 I tried alerting the response and it returns true if user-name and password is correct and returns false if incorrect. 我尝试警告响应,如果用户名和密码正确,则返回true;如果错误,则返回false。

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 提前致谢

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. 尝试在Chrome浏览器中打开页面,然后打开开发者控制台以查看语法错误。

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);
    }

  });
});

}); });

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

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