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
https://forums.phpfreaks.com/topic/141463-trying-to-retrieve-session-variable/
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>

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>

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?

 

 

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.