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.