I am working on HTML5 App using Phonegap,
The database resides at my server. Registration and login is done by ajax post and I have a three different level of access for each user
Application Flow: Take the input from user and validate this at server end “database” and then display Result, it could be success or fail or type of the user.
The problem: Once I get the respond then how to redirect to another page and save the user name on the page and How to Log Out the user.
this is my code
index.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login/register</title>
<script language="JavaScript" type="text/javascript">
function ajax_post(){
var databox =document.getElementById("status");
var hr = new XMLHttpRequest();
var url = "login.php";
var u = document.getElementById("username").value;
var p = document.getElementById("password").value;
var f = document.getElementById("format").value;
var vars = "username="+u+"&password="+p+"&format="+f;
hr.open("POST", url, true);
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
hr.onreadystatechange = function() {
if(hr.readyState == 4 && hr.status == 200) {
var return_data = JSON.parse(hr.responseText);
if(return_data.Status == "success"){
if(return_data.type == "Collecting Data"){
//first type of user
window.location.href = "a.html";
}
else{
if(return_data.type == "Supervisor"){
//second type of user
}
else{
//third type of user
}
}
}
else{
if(return_data.Status == "fail"){
databox.innerHTML=" please try again ";
}
}
}//end if
}//end fuction
hr.send(vars); // Actually execute the request
}//end ajax_post function
</script>
</head>
<body>
<table width="400px">
<tr>
<td colspan="2" style="text-align:center">
<h3>Log in/Register</h3>
</td>
</tr>
<tr>
<td valign="top">
<label for="username">Username *</label>
</td>
<td valign="top">
<input id="username" name="username" type="text" maxlength="50" size="25"/>
</td>
</tr>
<tr>
<td valign="top">
<label for="password">Password *</label>
</td>
<td valign="top">
<input id="password" name="password" type="password" maxlength="50" size="25"/>
</td>
</tr>
<tr>
<td colspan="2" style="text-align:center">
<input id="format" name="format" type="hidden" value="json"/>
<input name="myBtn" type="submit" value="Login" onClick="javascript:ajax_post();">
</td>
</tr>
</table>
<br /><br />
<div id="status"></div>
</body>
</html>
and this is login.php
<?php
session_start();
$username = $_POST['username'];
$password = $_POST['password'];
$password = md5($password);
$format= $_POST['format'];
mysql_connect("localhost","user","pass") or die (mysql_error());
mysql_select_db("database name") or die ("no database");
$sqlString = mysql_query("SELECT * FROM staff WHERE username = '$username' AND password = '$password'");
$row = mysql_fetch_array($sqlString);
$num_results = mysql_num_rows($sqlString);
if($num_results ==1){
$Status='success';
$type=$row["type"];
$_SESSION['username'] = $username;
deliver_response($username,$password,$Status,$type,$format);
}
else{
$Status='fail';
$type="empty";
deliver_response($username,$password,$Status,$type,$format);
}
function deliver_response($u,$p,$s,$t,$f){
if($f == 'json') {
$response['username']=$u;
$response['password']=$p;
$response['type']=$t;
$response['Status']=$s;
header('Content-type: application/json');
$json_response=json_encode($response);
echo $json_response;
}//end if
else{
echo 'format not valid';
}//end else
}//end function
?>
when i back with response and redirect to another html page and go to php file throw ajax and use this statement in php
echo $_SESSION['username'];
it will print null.