NorKayak 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; } Quote Link to comment https://forums.phpfreaks.com/topic/294177-variable-conversion-php-to-javascript/ Share on other sites More sharing options...
cyberRobot 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); ?>; Quote Link to comment https://forums.phpfreaks.com/topic/294177-variable-conversion-php-to-javascript/#findComment-1503888 Share on other sites More sharing options...
NorKayak 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... Quote Link to comment https://forums.phpfreaks.com/topic/294177-variable-conversion-php-to-javascript/#findComment-1503890 Share on other sites More sharing options...
cyberRobot 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. Quote Link to comment https://forums.phpfreaks.com/topic/294177-variable-conversion-php-to-javascript/#findComment-1503891 Share on other sites More sharing options...
NorKayak 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... Quote Link to comment https://forums.phpfreaks.com/topic/294177-variable-conversion-php-to-javascript/#findComment-1503898 Share on other sites More sharing options...
Barand Posted January 23, 2015 Share Posted January 23, 2015 $_SESSION variables are global. Have you got session_start() at the top of the page? Quote Link to comment https://forums.phpfreaks.com/topic/294177-variable-conversion-php-to-javascript/#findComment-1503899 Share on other sites More sharing options...
cyberRobot 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. Quote Link to comment https://forums.phpfreaks.com/topic/294177-variable-conversion-php-to-javascript/#findComment-1503900 Share on other sites More sharing options...
NorKayak 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(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/294177-variable-conversion-php-to-javascript/#findComment-1503901 Share on other sites More sharing options...
NorKayak 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? Quote Link to comment https://forums.phpfreaks.com/topic/294177-variable-conversion-php-to-javascript/#findComment-1503902 Share on other sites More sharing options...
cyberRobot 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); Quote Link to comment https://forums.phpfreaks.com/topic/294177-variable-conversion-php-to-javascript/#findComment-1503907 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.