I'm trying to create a login script where the user will enter their username and password. This will be sent via ajax to a php script to check if the details are correct. If they are the user should then be able to select a site, which ive been trying to display like this $('#logonContainer').load("login_DisplaySelectSite.php");
My problem is that I cant figure out how to run this code if the session has started with a valid username. Heres my code so far, maybe someone can help me!
html/jquery mobile:
<div data-role="content">
<form id='login' onsubmit="return false;">
<div id='logonContainer' style="background-color: #F8F4FA; padding: 10px; width:70%; margin: 0 auto; margin-top:15%;">
<input type="text" name='user' id='user' placeholder='Username'>
<input type="password" name='password' id='password' placeholder='Password'>
<input type="submit" id="submitLogin" name="login" value="Login" class='ui-btn ui-btn-c ui-btn-inline' data-theme='c' data-transition='pop'>
</div>
</form>
<div id="resultLog"></div>
</div>
ajax:
$(function userLogin() {
$("#submitLogin").click(function() {
var theUser = $.trim($("#user").val());
var thePassword = $.trim($("#password").val());
if(theUser.length > 0)
{
$.ajax({
type: "POST",
url: "callajax.php",
data: ({usr: theUser, pwd: thePassword}),
cache: false,
dataType: "text",
success: onSuccess
});
}
});
$("#resultLog").ajaxError(function(event, request, settings, exception) {
$("#resultLog").html("Error Calling: " + settings.url + "<br />HTTP Code: " + request.status);
});
function onSuccess(data)
{
$("#resultLog").html(data);
}
This next bit is not working at all!! This is where I'm trying to display a radio button list to replace the username and passowrd fields. After this I guess I will use a similar function to select the site, again, dependant on the session being active - once all these checks have been done the user should be sent to the home page
if(onSuccess(data) == 1)
{
$('#logonContainer').load("login_DisplaySelectSite.php");
} else {
}
});
php:
session_start();
if($_REQUEST['usr']=="admin" && $_REQUEST['pwd']=="1234"){
$_SESSION['USERNAME'] = "admin";
$_SESSION['SESSIONID']='123456';
echo "Login CORRECT WOOOOOOOO!! </br>" ;
//print json_encode($_SESSION);
print $_SESSION['USERNAME'];
$loggedon=1;
echo json_encode( $loggedon);
}
else{
echo "Incorrect Login :(" ;
$loggedon=0;
echo json_encode($loggedon);
}
Thanks