0

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>
2
Contributors
2
Replies
18
Views
3 Years
Discussion Span
Last Post by joachimanderson
0

What's ajaxObj("POST", "login.php") ? Does your ajax actually working ? You can track Ajax connection on your browser add-on like Firebug.

Are you sure your form data is passed to your Ajax page ? Also there is no </br> tag. It's just <br />

0

Thanks for your reply. Yes it does work but I have made changes (what you see above) to my original code from line 70. This was my attempt to forward to alternative pages. Here is the code (that works) just from line 70 before I tinkered with it.

                if(ajax.responseText == "login_failed"){
                    _("status").innerHTML = "Login unsuccessful, please try again.";
                    _("loginbtn").style.display = "block";
                } else {
                    window.location = "user.php?u="+ajax.responseText;
                }
            }
        }
        ajax.send("e="+e+"&p="+p);
    }
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.