weixin_33698043 2015-06-10 11:59 采纳率: 0%
浏览 19

Ajax / PHP登录没有反应

I have received a partially built website and was asked to implement a login system for clients and admins. I am quite new to JS and AJAX and I am having a hard time understanding what the other person has implemented and how I can get it to function properly.

This is the login form where the user can enter in the credientials.

<form name="ajaxform" id="ajaxform" action="auth/ajax-auth.php" method="POST" >
    <h4>Client Login</h4>
        <div id="error"></div>
            <label for="username" class="col-sm-2 control-label">Username</label>
            <input id="username" class="form-control" name='username' placeholder="Username">
            <label for="password" class="col-sm-2 control-label">Password</label>
            <input type="password" class="form-control" id="password" name='password' placeholder="Password">
        </div>
        <div class="modal-footer">
            <button type="button">Close</button>
            <button type="submit" id="loginbtn">Login</button>
        </div>
</form>

Once the form gets submitted it will run ajax-auth.php and also will run an Ajax function. It should then redirect the user who is logging in to either the client side or admin side dependant on their permission level.

The Ajax function looks like this:

$("#ajaxform").submit(function(e)
                      {
var username=$("#username").val();
var password=$("#password").val();
var dataString = 'username='+username+'&password='+password;
if($.trim(username).length>0 && $.trim(password).length>0) {
    $.ajax({
        type: "POST",
        url: "auth/ajax-auth.php",
        data: dataString,
        cache: false,
        beforeSend: function(){ $("#loginbtn").val('Connecting...');},
        success: function(data){
            if(data) {
                alert(data + " Logged in!");

               if(data == "admin"){
                   window.location.href = "/admin/admin.php";
               }
               else if(data == "client"){

                //uncomment when client_home.php page is ready
                //window.location.href = "client_home.php";

                //for now just redirect to homepage
                window.location.href = "index.html";
               }

            } else {
                $("#error").html("<span style='color:#cc0000'>Error:</span> Invalid username and password. ");
                $("#loginbtn").val('Submit')
            }
        }
    });
}

And here is my PHP:

session_start();

error_reporting(E_ALL);                
ini_set('display_errors', 1);          

$username = $_POST['username'];
$password = $_POST['password'];

include '../connection.php';
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

$sql = "SELECT * FROM users WHERE username='$username' AND password='$password'";

$result = mysqli_query($dbc, $sql);

    if (mysqli_num_rows($result) == true)
    {
        $_SESSION["username"] = $username;

        $row = mysqli_fetch_array($result);
        if ($row["permission"] == 2)
        {
         $_SESSION["permission"] = 2;
         //Redirect to the admin login page.
         header("Location: ../admin/admin.php");
        }
        else if ($row["permission"] == 1)
        {
        $_SESSION["permission"] = 1;
        $_SESSION["userId"] = $row["user_id"];
        $_SESSION["account"] = $row["account_id"];
        //redirect to the client home page.
        header("Location: ../index.html");   //Change this when ready, for now go to homepage
        }
    }
    else
    {
        echo "User not found!";
        header("Location: ../index.html");
    }

When I enter in a username and password (wrong or right) nothing is happening. If the credentials are wrong my error message should be hitting, and if I am logging in as admin I should be redirected to the admin page, and same with for the client being redirected to the client page.

How/Where am I going wrong here?

The console is not throwing me any errors and even my error message for invalid login creds is not being displayed.

I know I am close, any help is greatly appreciated.

  • 写回答

3条回答 默认 最新

  • weixin_33688840 2015-06-10 12:01
    关注

    You've to give some output in your PHP, because now you're redirecting the request. This won't work for an AJAX request.

    For example:

        if ($row["permission"] == 2)
        {
          $_SESSION["permission"] = 2;
          // Redirect to the admin login page.
          echo 'admin';
        }
        else if ($row["permission"] == 1)
        {
           $_SESSION["permission"] = 1;
           $_SESSION["userId"] = $row["user_id"];
           $_SESSION["account"] = $row["account_id"];
           echo 'client';
        }
    
    评论

报告相同问题?

悬赏问题

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