Login using ajax


#1

Hi All,

I’m using ionic to develop an app, it has login function. I didn’t use angularJS, all I used are Html, Js, Php. right now, I have a js functions loginFunction and readState to send user information from index.html to server, and get login state back to index.html.

in my php code i have 3 SESSION variables, and I assign them value when I use loginFunction, and I use readState to get back the SESSION variables

But the problem is that, When I test in browser, it shows me that the SESSION variables are saved. BUT when I use ionic view to emulate on the phone. the SESSION variables are empty! here are the codes.

What is the real problem? should I use angularJS other than ajax?

loginFunction (error function did not work)

function loginFunction(){           
var userName = document.getElementById("userName").value;
var password  = document.getElementById("password").value;
//return message when success
var data_string = 'userName=' + userName + '&password=' + password;
if(userName == '' || password ==''){
    alert("Please Fill All Field");
} else {
    alert(data_string);
    // ajax code for submit form
    $.ajax({
        type:   "POST",
        //use full address
        url:    "myurl/login.php",
        data:   data_string,
        cache:  false,
        beforeSend: function(){ alert("before send");},
        success: function(html){
                    alert("success");       
                    alert("im in the login function, will print login php print statements: " + html);
                }           
        error: function (xhr) {
                alert(xhr.status);
                }           
    }); 
}
return false;

}
my readState function

function readState(){

var xmlhttp;
if (window.XMLHttpRequest){
    //code for the IE7+,Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
}
else {
    //code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
    if (xmlhttp.readyState==4 && xmlhttp.status==200){
        //how to get login state???
        var str = xmlhttp.responseText;
        res = str.split(" ");
        // res[0] = login_state + res[1]= user_status + res[2]= username
        alert("res: " + res);       
        if (res[0] == "0" ) { //res[0] = 0 not login
            alert("please login"); //delete after publish                               
        } else if (res[0] == "1") { //res[0] = 1 logged in

            if( res[1] == '1'){ //res[1] = 1 login as employee
                alert("login as admin res[1] = "+ res[1] + "name is res[2]" + res[2]);
                window.location.href = "employee.html";
            } else if(res[1] == '2'){ //res[1] = 2 login as admin
                alert("login as admin res[1] = "+ res[1] + "name is res[2]" + res[2]);
                window.location.href = "admin.html";
            } else if(res[1] == '3'){ //res[1] = 3 login as client
                alert("login as admin res[1] = "+ res[1] + "name is res[2]" + res[2]);
                window.location.href = "client.html";
            }

        }   
    }
}
//true/false
xmlhttp.open("GET","myurl/login.php",true);
xmlhttp.send();

}
php, session varialbe, print it so I can use getState function

$_SESSION[“loginState”];
$_SESSION[“userStatus”];
$_SESSION[“userName”];

print $_SESSION[“loginState”];
print $_SESSION[“userStatus”];
print $_SESSION[“userName”];
assign value to them

if ($result) {
    //print "inside if 2 "; 
    $dataset = mysqli_fetch_assoc($result);

    if ($dataset["status"] == 1){
        //print "inside of 1 "; 
        $_SESSION["loginState"]= "1";
        $_SESSION["userStatus"] = $dataset["status"];
        $_SESSION["userName"] = $dataset["name"];
    } else if ($dataset['status'] == 2){
        //print "inside of 2 "; 
        $_SESSION["loginState"]= "1";
        $_SESSION["userStatus"] = $dataset["status"];
        $_SESSION["userName"] = $dataset["name"];
    } else if ($dataset['status'] == 3){
        //print "inside of 3 "; 
        $_SESSION["loginState"]= "1";
        $_SESSION["userStatus"] = $dataset["status"];
        $_SESSION["userName"] = $dataset["name"];       
    }