I have this code that loads on document.ready and it checks if the user is logged in or not:
//Checking if the user is logged in or not
$(function(){
$.getJSON("inc/API.php", {command : "getUserName"},
function(result){
if(result==null){
$("#divGreeting").html("Hello, guest!");
}
else {
$("#divGreeting").html("Hello, "+result+"!");
$("#divHeader").html("Hello, "+result+"! <a href='javascript:logout()'>Logout</a>");
}
});
});
If he's not - it says "Hello, guest"; if he is - it says "Hello, username", where username is the result in this function. What I need, is to make the username in this part: $("#divGreeting").html("Hello, "+result+"!"); to be a link, which leads to another page with userID, which I don't have. Like this: $("#divGreeting").html("Hello, <a href='userpage.html?userid="+HAVE TO GET THE ID HERE+"'>"+result+"</a>!");
I tried to use this function (it does get the:
function getUserID(){
$.getJSON("inc/API.php",
{
command : "getUserID"
},
function(result){
return result;
});
}
and to call it like that: $("#divGreeting").html("Hello, <a href='userpage.html?userid="+getUserID()+"'>"+result+"</a>!");, but that didn't work, I didn't get any result, except for undefined (I understand it has something to do with asynchronous AJAX).
This is the content of API.php:
case "getUserID":
echo getUserID();
break;
it call's for function getUserID() from file BusinessLogic.php, which has this function:
function getUserID()
{
$arr = select("SELECT userID FROM users WHERE username='".$_SESSION["username"]."'");
return $arr[0]["userID"];
}
I've been told that I have to use callbacks, but I have no idea how to do that, how to write this callback. I'm lost here...
Some help, please?