lynxus Posted May 28, 2009 Share Posted May 28, 2009 Hi guys, I have a Ajax request like below, However i want it to send a PHP var . How can i do this? Below is a quick thing showing the php var. Further down i sthe Javascript var i want to populate with the php value. var siteid = ( THIS IS WHERE I WANT TO INSERT THE PHP SESSION VALUE ) How do i do this? Ive tried doing inline php but that seems to fail , Im assuming because its between script tags.. Humm i suppose i could use php to echo the entire script with teh values included? Whats the *best way to do this? <? $_SESSION['siteid'] = "1234"; ?> <script language="javascript" type="text/javascript"> <!-- function getdata() { var ajaxRequest; try { // Opera 8.0+, Firefox, Safari ajaxRequest = new XMLHttpRequest(); } catch (e) { // Internet Explorer Browsers try { ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { // Something went wrong alert("Your browser broke!"); return false; } } } // Create a function that will receive data sent from the server ajaxRequest.onreadystatechange = function() { if(ajaxRequest.readyState == 4) { var ajaxDisplay = document.getElementById('sessions'); var result = ajaxRequest.responseText; ajaxDisplay.innerHTML = result; } } var nocache = Math.random(); var siteid = ( THIS IS WHERE I WANT TO INSERT THE PHP SESSION VALUE ) var queryString = "?siteid=" + siteid ; ajaxRequest.open("GET", "agetdata.php" + queryString, true); ajaxRequest.send(null); } //--> </script> Thanks G Quote Link to comment https://forums.phpfreaks.com/topic/160045-put-a-_session-var-in-javascript/ Share on other sites More sharing options...
akitchin Posted May 28, 2009 Share Posted May 28, 2009 1. use code tags please, as it makes everyone's lives easier and inhibits ocular bleeding. 2. you can echo a PHP variable into javascript: var siteid = <?php echo $_SESSION['siteid']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/160045-put-a-_session-var-in-javascript/#findComment-844321 Share on other sites More sharing options...
lynxus Posted May 28, 2009 Author Share Posted May 28, 2009 Hi, sorry Yeah i thought i could, however i tried and could see it in the HTML source, but the code failed Hummmm Ty for the idea. Quote Link to comment https://forums.phpfreaks.com/topic/160045-put-a-_session-var-in-javascript/#findComment-844324 Share on other sites More sharing options...
Philip Posted May 28, 2009 Share Posted May 28, 2009 Make sure to use full tags (<?php ) instead of short tags (<? <?= ) Make sure to have session_start(); at the very tip top of your script Quote Link to comment https://forums.phpfreaks.com/topic/160045-put-a-_session-var-in-javascript/#findComment-844325 Share on other sites More sharing options...
lynxus Posted May 28, 2009 Author Share Posted May 28, 2009 Yeah i tried: var nocache = Math.random(); var siteid = "<?php echo $_SESSION['siteid']; ?>"; var queryString = "?siteid=" + siteid; ajaxRequest.open("GET", "agetdata.php" + queryString, true); ajaxRequest.send(null); But this doesnt work. Nothing happens when the JS is run. However if i put the whole lot in PHP and echo the script with the bits i want it works. Looking at the source , BOTH html outputs are the same. Quote Link to comment https://forums.phpfreaks.com/topic/160045-put-a-_session-var-in-javascript/#findComment-844326 Share on other sites More sharing options...
Philip Posted May 28, 2009 Share Posted May 28, 2009 Make sure to have session_start(); at the very tip top of your script Also, above that put error_reporting(E_ALL); Quote Link to comment https://forums.phpfreaks.com/topic/160045-put-a-_session-var-in-javascript/#findComment-844329 Share on other sites More sharing options...
lynxus Posted May 28, 2009 Author Share Posted May 28, 2009 ok, done that. No errors show. Its getting the data fine as on a show source of the page it shows its inserted the code to the javascript correctly. However it doesnt seem to run if i do it this way.. Hmmmmmmmmmmmmmmmmmmmmmm Quote Link to comment https://forums.phpfreaks.com/topic/160045-put-a-_session-var-in-javascript/#findComment-844330 Share on other sites More sharing options...
lynxus Posted May 28, 2009 Author Share Posted May 28, 2009 even doing: var nocache = Math.random(); <?php echo 'var siteid = "'.$_SESSION['siteid'].'";'; ?> var queryString = "?siteid=" + siteid; Doesnt work. Heres a copy of the actualy output into the html source: var nocache = Math.random(); var siteid = "1234567890"; var queryString = "?siteid=" + siteid; Quote Link to comment https://forums.phpfreaks.com/topic/160045-put-a-_session-var-in-javascript/#findComment-844337 Share on other sites More sharing options...
lynxus Posted May 28, 2009 Author Share Posted May 28, 2009 Humm, Ive given up and decided to just echo the entire javascript function with the bits i want in it. This seems to work. Doesnt look like it likes having <?php ?> inside of the <script> tags. So i just echo the entire script built with teh vars included. Quote Link to comment https://forums.phpfreaks.com/topic/160045-put-a-_session-var-in-javascript/#findComment-844342 Share on other sites More sharing options...
Ken2k7 Posted May 28, 2009 Share Posted May 28, 2009 Does the file have a PHP extension on it? Quote Link to comment https://forums.phpfreaks.com/topic/160045-put-a-_session-var-in-javascript/#findComment-844344 Share on other sites More sharing options...
akitchin Posted May 28, 2009 Share Posted May 28, 2009 Does the file have a PHP extension on it? that would be a yes, of course, because he does in fact see the siteid in the source code. it's just not passing through the rest of the JS function properly. lynx, have you tried alert()ing the value of the querystring to see if it contains what it should? i know you've given up, but we might as well try and sort this out because there's no use forcing PHP to echo it all if you don't have to. Quote Link to comment https://forums.phpfreaks.com/topic/160045-put-a-_session-var-in-javascript/#findComment-844350 Share on other sites More sharing options...
lynxus Posted May 28, 2009 Author Share Posted May 28, 2009 Yeah. PHP works fine on the file. At the moment i have php section echoing the entire javascript bit, with the vars i want in it. Cant seem to get inline PHP to work , so this is the only option i have at the mo. Quote Link to comment https://forums.phpfreaks.com/topic/160045-put-a-_session-var-in-javascript/#findComment-844351 Share on other sites More sharing options...
lynxus Posted May 28, 2009 Author Share Posted May 28, 2009 hat would be a yes, of course, because he does in fact see the siteid in the source code. it's just not passing through the rest of the JS function properly. lynx, have you tried alert()ing the value of the querystring to see if it contains what it should? i know you've given up, but we might as well try and sort this out because there's no use forcing PHP to echo it all if you don't have to. Ill give that a try . Quote Link to comment https://forums.phpfreaks.com/topic/160045-put-a-_session-var-in-javascript/#findComment-844352 Share on other sites More sharing options...
Ken2k7 Posted May 28, 2009 Share Posted May 28, 2009 Sorry about that. So in what way is it not working. Does the AJAX work if you just inline the value "1234567890" into siteid instead of using PHP like so - var siteid = "1234567890"; Does the AJAX work? If not, maybe it's your agetdata.php that you should be looking at. Quote Link to comment https://forums.phpfreaks.com/topic/160045-put-a-_session-var-in-javascript/#findComment-844354 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.