使用jQuery Ajax和PHP登录过程


Login Process with jQuery Ajax and PHP

嘿,伙计们,我需要一些帮助。

我的问题是错误div没有显示我希望它出现在AJAX请求的成功函数中的内容。我尝试提醒响应,如果用户名和密码正确,它将返回true,如果不正确,则返回false。

我不知道为什么它不起作用。

这是我的代码

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

提前感谢

您编写的脚本中有一个额外的右括号。尝试在chrome中打开页面并打开开发人员控制台以查看语法错误。

Login.html:44 Uncaught SyntaxError: Unexpected token )

我将语法更正为以下

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

});