1

My issue is with my login page, I know its nothing fancy this is for testing purposes. My error is once I enter my details in it should send them off to the server to verify them with the database, then redirect me to the next page.

I know the server side code works as I have tested this with Postman and it return true. My problem I think comes when I call the $srvmsg, this is due to the fact every time I hit submit it sends me the error message "Error Username and Password Incorrect".

My code:

<div class="container">

    <fieldset>
    <legend>Login</legend>
    <h2 class="form-signin-heading">Please sign in</h2>
            <br>
        <label for='username' class="sr-only">Username:</label>
        <input class="form-control" type='text' name='username' id='username'  maxlength="50" placeholder="Username" />
            <br>
        <label for='password' class="sr-only" >Password*:</label>
        <input class="form-control" type='password' name='password' id='password' maxlength="50" placeholder="Password"/>
            <br>
        <input class="btn btn-lg btn-primary btn-block" type='button' name='submit' id="submission" value='Submit' />

    </fieldset>

<!-- //container  -->
</div> 
<script type="text/javascript">

$(function(){
$("#submission").click(function(){
    var $id = $("#username").val();
    var $pass = $("#password").val();
    var $msg = "";
    var $srvmsg = "";
    if($.trim($id).length === 0 ) $msg+="- Username is requried\n";
    if($.trim($pass).length === 0 ) $msg+="- Password is required\n";
    if($msg === ""){
        $srvmsg = $.ajax({
           type : 'post',
           async : false,
           data : {
               "username" : $id,
               "password" : $pass
           },
           url : "http://localhost/login"
           }).done(function(data){
           if(data.loggedIn === true){
               window.location = "http://localhost/my-app/www/home.html";
           } else {
               alert("Error Username and Password Incorrect");
           }
        });

    } else {
        alert("Errors on the form : \n"+$msg);
    }
 });
});
</script>

To help, this is also my server side code, I have tested this but hopefully it may give a better idea whats going wrong.

Server Side Code:

session_start();
if(isset($_POST)){
$username =  $_POST['username'];
$password =  $_POST['password'];
$msg = array();
if ($username&&$password){
    $connect = mysql_connect("127.0.0.1:8889","root","root") or die ('Couldnt Connect!!');
    mysql_select_db("AppDatabase") or die ('Couldnt find DB');
    $query = mysql_query("SELECT * FROM users WHERE username = '$username'");
    $numrows = mysql_num_rows($query);

    if($numrows !=0)
    {
        while($row = mysql_fetch_assoc($query))
            {
                $dbusername = $row['username'];
                $dbpassword = $row['password'];
                $_SESSION['clientid'] = $row['user_id'];
            }

        if($username==$dbusername&&$password==$dbpassword)
            {
                $_SESSION["loggedIn"] = true;
                $msg['loggedIn'] = true;
                $msg['client_id'] = $_SESSION['clientid'];
                $msg['message'] = "welcome!";

            } else {
                $msg['client_id'] = false;
                $_SESSION["loggedIn"] = false;
                $msg['loggedIn'] = false;
            };
    }else{
        $msg['message'] = 'Username or Password entered incorrectly';
    }
} else {
     $msg['message'] = 'You have not entered username or password';
}
echo json_encode($msg);  
}

Update

I have added type : 'post', and I have still be receiving the same error. I am unsure if $srvmsg is being passed username and password.

Thank you for you time.

Jonck
  • 1,587
  • 1
  • 10
  • 15
  • use dataType: "POST", – Saty Apr 20 '15 at 13:41
  • Unfortunately I have added `type: 'post'` it still gives me the same error. Through some testing Im not sure if the username and password is being passed into $srvmsg thus nothing will happen on the server side then give me the error message because loggedIn will = false. – Jonck Apr 20 '15 at 15:28
  • **Danger**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Apr 20 '15 at 15:38
  • You need to do some debugging beyond recognising that the error branch of your Ajax function is firing. Look at your browser's developer tools. Look at the JavaScript console. Does it report any errors? Look at the Net tab. Is the request being made? Does it get a response? Do they contain the data you expect? – Quentin Apr 20 '15 at 15:38
  • Hey @Quentin I am using a different connection in the rest of my system this was a quick mock to test how to login thank you for mentioning though. Also I am still debugging the issue just hoping someone may of had the same issue or can spot a problem Im missing. – Jonck Apr 20 '15 at 15:52

1 Answers1

2

use type: 'post' as of jQuery ajax documentation

Celoain
  • 135
  • 7