Reputation: 1496
I've got two functions which both get the user_id of a user. One function gets the session user_id and the other function gets the id that's in the url like so: profile.html?id=123. Here's the code:
// get URL parameter (user_id)
function GetURLParameter(sParam) {
var sPageURL = window.location.search.substring(1);
var sURLVariables = sPageURL.split('&');
for (var i = 0; i < sURLVariables.length; i++)
{
var sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] == sParam)
{
return sParameterName[1];
}
}
}
// get URL parameter (user_id) function call
url_user_id = GetURLParameter('id');
alert("url user id = " + url_user_id);
// get SESSION user_id
function get_session_user_id() {
$.post( "http://localhost/snapll_back/account/session_user_id.php",
function(info) {
if(info != "Something went wrong. ERROR{1_RAI_ERROR}") {
session_user_id = info;
alert("session user id = " + session_user_id);
// hide the follow button if this me is owned by the viewer
if(session_user_id == url_user_id) {
$("#follow_button").hide();
}
// THE QUESTION ON S.O. IS ABOUT THIS IF STATEMENT
// give the url_user_id variable the value of the session_user_id variable if not set correctly via the URL
if(url_user_id == 'undefined' || url_user_id == "" || url_user_id == null) {
url_user_id = session_user_id;
}
}
else {
$('#warning').html(info).fadeIn(200);
setTimeout(function () {
window.location.href = "index.html";
}, 2000);
}
});
return false;
}
get_session_user_id();
So, what I need to do is to check if the variable 'url_user_id' is set. If not, the url_user_id should get the same value as the variable 'session_user_id'. However, for some reason, this doesn't happen. I have the if statement in the code above in this part:
if(url_user_id == 'undefined' || url_user_id == "" || url_user_id == null) {
url_user_id = session_user_id;
}
I use the url_user_id variable to get the username of the current profile the user is viewing. Here's the function that gets the username:
// get username function
function get_username() {
$.post( "http://localhost/snapll_back/account/username.php?id="+url_user_id,
function(info) {
if(info != "Something went wrong. ERROR{1_RAI_ERROR}") {
$("#main_header_username").html(info);
}
else {
$('#warning').html(info).fadeIn(200);
}
});
return false;
}
When I visit the page with the url like this: www.mywebsite.com/profile.php?id= (so if I specify no id in the URL) the variable url_user_id, which normally gets its value from the URL, should get the value of the variable session_user_id. Currently, I keep getting 'undefined' as the value of url_user_id and the if statement doesn't seem to notice that value to take action.
Who knows how I can fix this?
+=======================================================================================+ EDIT
I'm now able to give the url_user_id the right value, but my function get_username() can't access that value. My get_session_user_id function now looks like this:
// get SESSION user_id
function get_session_user_id() {
$.post( "http://localhost/snapll_back/account/session_user_id.php",
function(info) {
if(info != "Something went wrong. ERROR{1_RAI_ERROR}") {
session_user_id = info;
alert("session user id = " + session_user_id);
// if url_user_id is not defined by the URL give it the value of session_user_id
if(url_user_id == 'undefined' || url_user_id == "" || url_user_id == null) {
url_user_id = session_user_id;
alert("url user id = " + url_user_id);
}
// hide the follow button if this me is owned by the viewer
if(session_user_id == url_user_id) {
$("#follow_button").hide();
}
}
else {
$('#warning').html(info).fadeIn(200);
setTimeout(function () {
window.location.href = "index.html";
}, 2000);
}
});
return false;
}
It alerts url_user_id with the correct value when no id is specified in the URL, but the get_userame function still says 'undefined' when I alert it in that function. Why can't the get_username function get the most recent value of url_user_id?
Upvotes: 0
Views: 68
Reputation: 1108
It looks like you're calling get_username immediately, before get_session_user_id has had a chance to return.
Try doing this:
// get URL parameter (user_id)
function GetURLParameter(sParam) {
var sPageURL = window.location.search.substring(1);
var sURLVariables = sPageURL.split('&');
for (var i = 0; i < sURLVariables.length; i++)
{
var sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] == sParam)
{
return sParameterName[1];
}
}
}
// get URL parameter (user_id) function call
url_user_id = GetURLParameter('id');
// get SESSION user_id
function get_session_user_id() {
$.post( "http://localhost/snapll_back/account/session_user_id.php",
function(info) {
if(info != "Something went wrong. ERROR{1_RAI_ERROR}") {
session_user_id = info;
//alert("session user id = " + session_user_id);
// if url_user_id is not defined by the URL give it the value of session_user_id
if(url_user_id == 'undefined' || url_user_id == "" || url_user_id == null) {
url_user_id = session_user_id;
//alert("url user id = " + url_user_id);
}
// hide the follow button if this me is owned by the viewer
if(session_user_id == url_user_id) {
$("#follow_button").hide();
}
}
else {
$('#warning').html(info).fadeIn(200);
setTimeout(function () {
window.location.href = "index.html";
}, 2000);
}
// get username function call
get_username();
});
return false;
}
get_session_user_id();
// get username function
function get_username() {
//alert(url_user_id);
$.post( "http://localhost/snapll_back/account/username.php?id="+url_user_id,
function(info) {
if(info != "Something went wrong. ERROR{1_RAI_ERROR}") {
$("#main_header_username").html(info);
}
else {
$('#warning').html(info).fadeIn(200);
}
});
return false;
}
/*
* function calls
*/
// check session function call
check_session();
We've moved the call to get_username
inside the success callback to your POST, but otherwise the code is the same.
Upvotes: 1