I'm really hoping someone can help and I should warn you I am javascript novice.
I have a login script for my members website that uses Ajax. It works just fine but I now want to be able to redirect the user to a different page depending whether their account type is "A" or "B", (this variables are stored in a mysql database"). I can't figure this out and I'm hoping someone might tell me where I'm going wrong. Thank you in advance.
Joachim
<?php
// AJAX CALLS THIS LOGIN CODE TO EXECUTE
if(isset($_POST["e"])){
// CONNECT TO THE DATABASE
include_once("db_conx.php");
// GATHER THE POSTED DATA INTO LOCAL VARIABLES AND SANITIZE
$e = mysqli_real_escape_string($db_conx, $_POST['e']);
$p = md5($_POST['p']);
// GET USER IP ADDRESS
$accounttype = (['accounttype']);
$ip = preg_replace('#[^0-9.]#', '', getenv('REMOTE_ADDR'));
// FORM DATA ERROR HANDLING
if($e == "" || $p == ""){
echo "login_failed";
exit();
} else {
// END FORM DATA ERROR HANDLING
$sql = "SELECT id, username, accounttype, password FROM my_database WHERE email='$e' AND activated='1' LIMIT 1";
$query = mysqli_query($db_conx, $sql);
$row = mysqli_fetch_row($query);
$db_id = $row[0];
$db_username = $row[1];
$db_pass_str = $row[2];
if($p != $db_pass_str){
echo "login_failed";
exit();
} else {
// CREATE THEIR SESSIONS AND COOKIES
$_SESSION['accounttype'] = $db_accounttype;
$_SESSION['userid'] = $db_id;
$_SESSION['username'] = $db_username;
$_SESSION['password'] = $db_pass_str;
setcookie("id", $db_id, strtotime( '+30 days' ), "/", "", "", TRUE);
setcookie("user", $db_username, strtotime( '+30 days' ), "/", "", "", TRUE);
setcookie("pass", $db_pass_str, strtotime( '+30 days' ), "/", "", "", TRUE);
// UPDATE THEIR "IP" AND "LASTLOGIN" FIELDS
$sql = "UPDATE members SET ip='$ip', lastlogin=now() WHERE username='$db_username' LIMIT 1";
$query = mysqli_query($db_conx, $sql);
echo $db_username;
exit();
}
}
exit();
}
?>
<!DOCTYPE html>
<html>
<head>
<script src="js/main.js"></script>
<script src="js/ajax.js"></script>
<script>
function emptyElement(x){
_(x).innerHTML = "";
}
function login(){
var accounttype = "accounttype";
var e = _("email").value;
var p = _("password").value;
if(e == "" || p == ""){
_("status").innerHTML = "Fill out all of the form data";
} else {
_("loginbtn").style.display = "none";
_("status").innerHTML = 'please wait ...';
var ajax = ajaxObj("POST", "login.php");
ajax.onreadystatechange = function() {
if(ajaxReturn(ajax) == true) {
if(ajax.responseText == "login_failed"){
_("status").innerHTML = "Login unsuccessful, please try again.";
_("loginbtn").style.display = "block";
} if
(accounttype =="a"){
window.location = "http://www.yahoo.com";
} else if (accounttype =="b"){
window.location="http://www.google.com";
}
}
}
ajax.send("e="+e+"&p="+p);
}
}
</script>
</head>
</br>
</br></br>
</br></br>
</br></br>
</br>
<body>
<div id="pageMiddle">
<div class="container">
<!-- LOGIN FORM -->
<form class="form-signin" id="loginform" onsubmit="return false;">
<h2 class="form-signin-heading">Please sign in</h2>
</br>
<input type="text" class="form-control" id="email" onfocus="emptyElement('status')" maxlength="88" placeholder="Email address">
</br>
<input type="password" class="form-control" placeholder="Password" id="password" onfocus="emptyElement('status')" maxlength="100">
</label>
</br>
<button class="btn btn-primary" id="loginbtn" onclick="login()">Sign in</button>
</br>
<p id="status"></p>
</br>
<a href="#">Forgot Your Password?</a>
</form>
<!-- LOGIN FORM -->
</div>
</div>
</body>
</html>