i have a function
function chkLogin() {
$.get("setAlert.php", function (data) {
$(".result").html(data);
if (data == 'Not logged in') {
alert("not logged in.");
} else {
alert("success");
}
});
}
setAlert.php
if (!isset($login_session)) {
return "Not logged in";
}
else{
return "logged in";
}
and it alerts success because data is blank. So how can i set data to "Logged in " or "Not logged in" ? Please help.
You don't need to return anything
just use echo
if (!isset($login_session)) {
echo "Not logged in";
} else {
echo "logged in";
}
Read jQuery Ajax call tutorial
To redirect user if he is not logged in use Window.location
$.get("setAlert.php", function (data) {
$(".result").html(data);
if (data == 'Not logged in') {
window.location = "index.php"; // redirect page if user not logged in
} else {
alert("success");
}
});
if you want to check login. usually use the cookie to check. you could call the php cookie method to save cookie, and check the cookie data on client side.
try to handle response with json data.
call the ajax with json
function chkLogin() {
$.get("setAlert.php", function (data) {
$(".result").html(data);
if (data.status == true) {
alert("success");
} else {
alert("not logged in.");
}
},"json");
}
Modify setAlert.php
$result = array();
if (!isset($login_session)) {
$result['status'] = false;}
else{ $result['status'] = true;
}
echo json_encode($result);