1

I have a similar post AJAX + PHP login not working, but this time I want to add the authority or access level of each user.

Below is my code:

checklogin.php

<?php
session_start();
// Connect to db
include('db.php');

$stmt = $conn->prepare('SELECT * FROM users WHERE username = :username AND password = :password');
$stmt->execute(array(':username' => $_POST['username'], ':password' =>   $_POST['password']));

$result = $stmt->fetch(PDO::FETCH_ASSOC);
$affected_rows = $stmt->rowCount();
//$stmt->setFetchMode(PDO::FETCH_ASSOC);
//$affected_rows = $stmt->fetchColumn();

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


if($affected_rows == 1) {
    while($affected_rows = $result){
        //add the user to our session variables
        $_SESSION['username'] = $username;
        if($affected_rows['authority'] == 'admin'){
            $_SESSION['authority'] == 'admin';
            header("Location: main.php");
            exit;
        }
        if($affected_rows['authority'] == 'doctor'){
            $_SESSION['authority'] == 'doctor';
            header("Location: pages/doctor/index.php");
            exit;
        }
        if($affected_rows['authority'] == 'nurse'){
            $_SESSION['authority'] == 'nurse';
            header("Location: pages/nurse/index.php");
            exit;
        }
    }
    echo 'yes';
}

else {
    echo 'no';
} // end of $affected_rows


 ?>

index.php

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script>
<script language="javascript">  
    $(document).ready(function(){  
        $("#loginForm").submit(function(){  

            $("#report").removeClass().addClass('loader').html('<img src="images/ajax-loader.gif">').fadeIn(1000);  
            $.post("checklogin.php",{ username:$('#username').val(),password:$('#password').val()},function(data){  
                if(data=='yes'){  
                    $("#report").fadeTo(200,1,function(){       
                        $(this).html('Logging in.....').addClass('log').fadeTo(900,1,function(){          
                            document.location='main.php';  
                        });       
                    });  
                } else {  
                    $("#report").fadeTo(200,1,function(){        
                        $(this).html('Username or password error.').addClass('error').fadeTo(900,1);  
                    });    
                }  
            });  
            return false;   
        });  

        $("#password").blur(function(){  
            $("#loginForm").trigger('submit');  
        });  
    });  
</script>  


</head>

<body>

<div class="container">
  <form class="form-signin" action="checklogin.php" method="post" id="loginForm">
    <h2 class="form-signin-heading">User Login</h2>
    <input type="text" class="input-block-level" placeholder="Username" name="username" id="username">
    <input type="password" class="input-block-level" placeholder="Password" name="password" id="password">
    <label class="checkbox">
      <input type="checkbox" value="remember-me"> Remember me
    </label>
    <button class="btn btn-large btn-primary" type="submit" name="login" id="login">Sign in</button> 

    <div id="report"></div>

  </form>
</div> <!-- /container -->

When I input the wrong, it gives Username or password error, but when I input the correct it gives me Username or password error. Any ideas? I would gladly appreciate your help. Thanks.

0

1 Answer 1

1

Your AJAX response (PHP) shouldn't redirect. If you redirect, the response you return (e.g., "yes") will be lost because the browser will follow the redirect and give you the data from the destination page (e.g., the HTML from main.php).

Sign up to request clarification or add additional context in comments.

3 Comments

so what alternative should I do to avoid such response loss?
@neknekmouh 1) If you want the entire page to reload, just use a regular form submission and avoid AJAX altogether. 2) If you don't want a full page refresh, why are you redirecting? 3) As a kluge, you could return the destination URL as part of the AJAX response and make the redirect happen via JavaScript.
See also: The PHP function json_encode can help you return a more complex AJAX response that is easy to process in JavaScript. That way, your data variable could be an object like {success: true, redirect: 'path/to/destination.php'}.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.