Jump to content

Trying to retrieve Session Variable


XpertWorlock

Recommended Posts

Hi, I'm making a simple js function that retrieves Session variables.  It works when you assign the response to an ID, I just want to have it return.

 

function getSessionVariable(id)
{

var xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  } 
var url="getSessionVariable.php";
var params = 'id='+id+'&sid='+Math.random();
xmlHttp.open("POST",url,true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.setRequestHeader("Content-length", params.length);
xmlHttp.setRequestHeader("Connection", "close");

xmlHttp.onreadystatechange=function()
{

if (xmlHttp.readyState==4)
{ 
return xmlHttp.responseText

}

}

xmlHttp.send(params);



} 

 

I don't need to show you the PHP page it requests, because like I said, it works perfectly if assigning the response to an ID.

 

But if I write something like this in js

 

alert(getSessionVariable('userName'))

 

it pops up undefined, making me understand that the problem is "return xmlHttp.responseText"

 

 

 

Link to comment
Share on other sites

AJAX is asynchronous. this means that the getSessionVariable() will finish before the HttpRequest is complete. what this means is that you can't return from the onreadystatechange() function. instead, put what you want to do inside the if or use this, and put what you want it to do inside the onComplete() function:

<script>
function getSessionVariable(id)
{
  var xmlHttp=GetXmlHttpObject();
  if (xmlHttp==null)
  {
    alert ("Your browser does not support AJAX!");
    return;
  }
  var url="getSessionVariable.php";
  var params = 'id='+id+'&sid='+Math.random();
  xmlHttp.open("POST",url,true);
  xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xmlHttp.setRequestHeader("Content-length", params.length);
  xmlHttp.setRequestHeader("Connection", "close");
  
  xmlHttp.onreadystatechange=function()
  {
    if (xmlHttp.readyState==4)
    {
      onComplete(xmlHttp.responseText);
      return;
    }
  }

  xmlHttp.send(params);
}
function onComplete ( response ) {
  alert(response);
}
</script>

Link to comment
Share on other sites

p.s. - you don't need to add the random number, as POSTs are not cached. also, you can shorten this code up by using jquery:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script type="text/javascript">
function (id){
  $.post("getSessionVariable.php", { 'id': id }, function ( data ){
    alert(data);
  });
}
</script>

Link to comment
Share on other sites

Thanks for the POST cache info, didn't think of that.  I see what you mean that getSessionVariable() ends before the HttpRequest, is there anyway of getting it like this than:

 

If $_SESSION['userName'] = "Jeff";

 

 

if I write in JS

 

var userName = getSessionVariable('userName');
document.write(userName);

I want it to write Jeff, or alert, etc.  Trying to make it open ended for whatever I need it to do.

 

Is this possible?

 

 

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.