XpertWorlock Posted January 19, 2009 Share Posted January 19, 2009 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" Quote Link to comment Share on other sites More sharing options...
rhodesa Posted January 19, 2009 Share Posted January 19, 2009 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> Quote Link to comment Share on other sites More sharing options...
rhodesa Posted January 19, 2009 Share Posted January 19, 2009 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> Quote Link to comment Share on other sites More sharing options...
XpertWorlock Posted January 19, 2009 Author Share Posted January 19, 2009 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? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted January 19, 2009 Share Posted January 19, 2009 no...the function that starts the AJAX can't return the value. you MIGHT be able to turn async off, but not sure. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.