NorKayak 0 Posted January 23, 2015 Share Posted January 23, 2015 Hi;I'm a beginner and I'm getting a big headache figuring how to convert php variables into Javascript variables.I want to use the content of $theName as the value for an input tag. $_SESSION['$Sess_Name'] is loaded with the right data, I verified it. But my function InitField returns always the word NULL.Why is that? Where is the problem?Thank Here is my code: function InitField() { <?php $theName = $_SESSION['$Sess_Name']; ?> var tempoVar = "<?php echo json_encode($theName); ?>"; var myInput = document.getElementById('theSurname'); myInput.value = tempoVar; } Link to post Share on other sites
cyberRobot 126 Posted January 23, 2015 Share Posted January 23, 2015 It looks like json_encode() adds the quotes for you. Try changing this: var tempoVar = "<?php echo json_encode($theName); ?>"; To this: var tempoVar = <?php echo json_encode($theName); ?>; Link to post Share on other sites
NorKayak 0 Posted January 23, 2015 Author Share Posted January 23, 2015 It looks like json_encode() adds the quotes for you. Try changing this: var tempoVar = "<?php echo json_encode($theName); ?>"; To this: var tempoVar = <?php echo json_encode($theName); ?>; Ok, I tried. Still not working. The diference is before I had the output "NULL" and now nothing... Link to post Share on other sites
cyberRobot 126 Posted January 23, 2015 Share Posted January 23, 2015 When viewing the page in your browser, does the correct name appear in the source code? Are you getting any JavaScript errors? In case you're not aware, you can right-click the page in a browser like Chrome. Then click the Inspect Element option. The errors would be displayed in the Console tab. Link to post Share on other sites
NorKayak 0 Posted January 23, 2015 Author Share Posted January 23, 2015 When viewing the page in your browser, does the correct name appear in the source code? Are you getting any JavaScript errors? In case you're not aware, you can right-click the page in a browser like Chrome. Then click the Inspect Element option. The errors would be displayed in the Console tab. Thanks I didn't think about that... I checked and the codes reads: var tempoVar = null; It seems then that the $_SESSION variable is not recognized in the function. Could that have something to do with declaring the variable global? So I tried this: <?php $theName = global $_SESSION['$Sess_Name']; ?> But the browser doesn't accept it... Link to post Share on other sites
Barand 1,621 Posted January 23, 2015 Share Posted January 23, 2015 $_SESSION variables are global. Have you got session_start() at the top of the page? Link to post Share on other sites
cyberRobot 126 Posted January 23, 2015 Share Posted January 23, 2015 Also, is your SESSION variable called "Sess_Name" or "$Sess_Name"? Note that your code adds a dollar sign to the name. Link to post Share on other sites
NorKayak 0 Posted January 23, 2015 Author Share Posted January 23, 2015 $_SESSION variables are global. Have you got session_start() at the top of the page? Yes: <?php ob_start(); session_start(); ?> Link to post Share on other sites
NorKayak 0 Posted January 23, 2015 Author Share Posted January 23, 2015 Also, is your SESSION variable called "Sess_Name" or "$Sess_Name"? Note that your code adds a dollar sign to the name. You mean I should remove the $ sign? Link to post Share on other sites
cyberRobot 126 Posted January 23, 2015 Share Posted January 23, 2015 You mean I should remove the $ sign? That depends. If your SESSION variable is named with a dollar sign, it should stay. I was just checking to see if the dollar sign was put there on purpose. <?php ob_start(); session_start(); ?> Did you turn off the output buffer after the session_start() call? That way you can see if PHP is throwing errors. If no errors are showing up, do you know if errors are being hidden by the server? Note that you can add the following lines at the top of your script...probably after your session_start() call: //REPORT ALL PHP ERRORS error_reporting(E_ALL); ini_set('display_errors', 1); Link to post Share on other sites
Recommended Posts
Archived
This topic is now archived and is closed to further replies.