khadra4ever Posted January 11, 2011 Share Posted January 11, 2011 Hi everybody! I am new to php, I'm trying to read a php variable using javascript function my code is as follow: <?php $php_var = 100; ?> <SCRIPT type="text/javascript"> var js_var = "<?php =$php_var; ?>"; function display_var() { alert(js_var); } </SCRIPT> <body onload="display_var()"> .......... .......... </body> the problem is that the line alert(js_var); gives me "<?php =$php_var; ?>" instead of 100. I don't know what I'm missing. any help would be greatly appreciated. Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/224050-read-php-variable-from-javascript/ Share on other sites More sharing options...
denno020 Posted January 11, 2011 Share Posted January 11, 2011 trying putting the javascript inside <?php ?> tags, and echoing it out to the page. Then you should be able to print the variable. The problem is that one is client side, and the other is server side. I'm not sure if this would work as expected, but it's worth a try. Denno Quote Link to comment https://forums.phpfreaks.com/topic/224050-read-php-variable-from-javascript/#findComment-1157762 Share on other sites More sharing options...
QuickOldCar Posted January 11, 2011 Share Posted January 11, 2011 <?php $php_var = 100; echo " <SCRIPT type='text/javascript'> var js_var = $php_var; function display_var() { alert(js_var); } </SCRIPT> <body onload='display_var()'> .......... .......... </body>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/224050-read-php-variable-from-javascript/#findComment-1157789 Share on other sites More sharing options...
khadra4ever Posted January 11, 2011 Author Share Posted January 11, 2011 Thank you guys for your replies QuickOldCar your solution worked fine thanks alot Quote Link to comment https://forums.phpfreaks.com/topic/224050-read-php-variable-from-javascript/#findComment-1157802 Share on other sites More sharing options...
khadra4ever Posted January 11, 2011 Author Share Posted January 11, 2011 Sorry guys i'm not done yet what i'm trying to do now is access that same php variable ($php_var) from an external js file which contains all my javascript code thanks! Quote Link to comment https://forums.phpfreaks.com/topic/224050-read-php-variable-from-javascript/#findComment-1157806 Share on other sites More sharing options...
denno020 Posted January 11, 2011 Share Posted January 11, 2011 lol, you're welcome... I told you what you should have done so you could learn for yourself, QuickOldCar merely gave you the code... Anyway for your next question, can't do it. You need to use AJAX. Again, that reason is to do with where the code is executed. Quote Link to comment https://forums.phpfreaks.com/topic/224050-read-php-variable-from-javascript/#findComment-1157879 Share on other sites More sharing options...
MatthewJ Posted January 11, 2011 Share Posted January 11, 2011 I think the only problem with the original code was var js_var = "<?php =$php_var; ?>"; should be var js_var = "<?=$php_var; ?>"; The quick tag syntax was incorrect... not sure it needed the whole script echo'd, but if it works Quote Link to comment https://forums.phpfreaks.com/topic/224050-read-php-variable-from-javascript/#findComment-1157881 Share on other sites More sharing options...
MatthewJ Posted January 11, 2011 Share Posted January 11, 2011 lol, you're welcome... I told you what you should have done so you could learn for yourself, QuickOldCar merely gave you the code... Anyway for your next question, can't do it. You need to use AJAX. Again, that reason is to do with where the code is executed. Couldn't you just set a cookie? Quote Link to comment https://forums.phpfreaks.com/topic/224050-read-php-variable-from-javascript/#findComment-1157886 Share on other sites More sharing options...
denno020 Posted January 11, 2011 Share Posted January 11, 2011 what about people who have cookies disabled, and whom are scared of cookies (yes I know people who know very little about computer and think that all cookies are viruses). Using AJAX should mean no one would have problems, no matter their security settings. Denno Quote Link to comment https://forums.phpfreaks.com/topic/224050-read-php-variable-from-javascript/#findComment-1157891 Share on other sites More sharing options...
MatthewJ Posted January 11, 2011 Share Posted January 11, 2011 What if those same people disable javascript? Quote Link to comment https://forums.phpfreaks.com/topic/224050-read-php-variable-from-javascript/#findComment-1157910 Share on other sites More sharing options...
denno020 Posted January 11, 2011 Share Posted January 11, 2011 Then those people don't deserve to see a good website lol. You're pretty much screwed. I guess it's up to the designer which minority they want to rule out of getting full functionality from the website.. Quote Link to comment https://forums.phpfreaks.com/topic/224050-read-php-variable-from-javascript/#findComment-1157911 Share on other sites More sharing options...
khadra4ever Posted January 12, 2011 Author Share Posted January 12, 2011 Thank you guys for your replies actually i don't know how to use cookies but i used text file instead i don't know if it's a good idea but it worked for me what i did is i had php to write data from database and write the results in a temporary text file then javascript reads that data into array, so it can populate a combobox. once again thank you guys! Quote Link to comment https://forums.phpfreaks.com/topic/224050-read-php-variable-from-javascript/#findComment-1158250 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.