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 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献