数据未通过Ajax登录

时间:2019-09-26 19:56:28

标签: php jquery ajax mysqli

我是PHPAjax的初学者。我正在使用session的两种语言开发MySql的登录表单,并同时观看视频教程。最终,我收到了错误消息,例如emailpassword错误,将其重定向到欢迎页面。

代码如下:

<style>
  #box{
    width:100%;
    max-width:500px;
    border:1px solid #ccc;
    border-radius:5px;
    margin:0 auto;
    padding:0 20px;
    box-sizing:boader-box;
    height:270px;
  }
</style>

<div id=box>
  <form method="POST" class="form-signin">
    <div class="account-logo">
      <img src="assets/img/logo-dark.png" alt="">
    </div>
    <div class="form-group">
      <label> Email</label>
      <input type="email" name="email" id="email" autofocus="" class="form-control">
    </div>
    <div class="form-group">
      <label>Password</label>
      <input type="password" name="password" id="password"class="formcontrol">
    </div>
    <div class="form-group text-right">
      <a href="forgot-password.html">Forgot your password?</a>
    </div>
    <div class="form-group text-center">
      <input type="button"  class="btn btn-primary account-btn" name="login" value="DO LOGIN" id="login_button">
    </div>
    <div id="error">       </div>
    <div class="text-center register-link">
      Don’t have an account? <a href="register.html">Register Now</a>
    </div>
  </form>
</div>

Ajax查询将是:

$(document).ready(function(){
  $('#login_button').click(function(){
    var email = $('#email').val();
    var password = $('#password').val();

    if($.trim(email).length>0 && $.trim(password).length>0 ){
      $.ajax({
        type: "POST",  // Type of request to be send, called as method
        url: "auth/logged.php", // Url to which the request is send
        data: {"email":email,"password":password},
        cache:false,
        beforeSend:function()
        {
          $('#login_button').val("connecting.....");
        },
        success: function(data)
        {
          if(data){
            $('body').load("index.php").hide().fadeIn(1500);
          }else{
            var option ={
              distace :'40',
              direction:'left',
              times:3
            }
            $('#box').effect('shake',option,800);
            $('#login_button').val('Login');
            $('#error').html("<span class='text-danger'> Invalid username or password </span>");
          }
        }
      });
    }else{
      return false;
    }
  });
});

logged.php将是

<?php
  include "dbconnection.php";
  session_start();

  if (isset($_POST['email'])  && isset($_POST['password']) ) {
    $email=mysqli_real_escape_string($conn,$_POST['email'] );
    $password=md5(mysqli_real_escape_string($conn,$_POST['password']) );
    $sql = "SELECT * FROM users WHERE email = '". $email."'  AND '". $password."'  ";
    $result= mysqli_query($conn,$sql);
    $num_row= mysqli_num_rows($result);

    if($num_row>0)
    {
      $data=mysqli_fetch_array($result);
      $_SESSION['username']=$data['username'];
     echo $data['username'];
    }
  }
?>

无论何时输入正确或错误的任何数据,它都会重定向到欢迎页面。请帮助我。

  • 列表项

1 个答案:

答案 0 :(得分:0)

在您的logging.php上尝试一下:

<?php
    include "dbconnection.php";
    session_start();
    if (isset($_POST['email'])  && isset($_POST['password']) ) {
        //i dont see your password colom name, so i guess it password
        if ($stmt = mysqli_prepare($conn, "SELECT username FROM users WHERE email=? AND password=?")) {
            $email = $_POST['email'];
            $password = md5($_POST['password']);
            mysqli_stmt_bind_param($stmt, "ss", $email, $password);
            mysqli_stmt_execute($stmt);
            mysqli_stmt_bind_result($stmt, $username);

            echo mysqli_stmt_fetch($stmt);
            $_SESSION['username']=$username;
            mysqli_stmt_close($stmt);
        }
    }
?>