niche Posted May 21, 2012 Share Posted May 21, 2012 (edited) Just curious. How do you turn a php var into js var and back? Using a format like this: <html><head><script type="text/javascript">function displayText() { var hello = ??? PHP HERE ??? alert(hello);}</script></head><body><button onclick="displayText()">Display</button></body></html> Edited May 21, 2012 by niche Link to comment Share on other sites More sharing options...
Guest So Called Posted May 21, 2012 Share Posted May 21, 2012 (edited) How about this? var hello = <?php echo $php_var; ?> The other way, probably requires AJAX technique. Edited May 21, 2012 by So Called Link to comment Share on other sites More sharing options...
niche Posted May 21, 2012 Author Share Posted May 21, 2012 Thanks, but no go. What do you think? Current Script: <html><head><script type="text/javascript">function displayText() { var hello = <?php echo $php_var; ?> alert(hello);}</script></head><body><?php $php_var = "Hello World!"; ?><button onclick="displayText()">Display</button></body></html> Link to comment Share on other sites More sharing options...
Guest So Called Posted May 21, 2012 Share Posted May 21, 2012 No, won't work. AFAIK you need AJAX to do what you want. PHP is good for inserting its variables into generated HTML. I can't see any way to go the reverse direction without AJAX. And worse, I know practically nothing about AJAX so I can't help you. I know most of what you can do with PHP and that ain't it. Link to comment Share on other sites More sharing options...
Ingolme Posted May 21, 2012 Share Posted May 21, 2012 PHP executes on the server. You can print things into the source code of the page, but once the source code is sent to the client side, that's it. You can print PHP data into Javascript source code, but in your case you had two mistakes: Echoing a variable before giving any data to it Not putting quotation marks around a string in Javascript. Here's an example of giving data to Javascript from PHP: var str = "<?php echo $str; ?>" In order to send data to PHP from Javascript, you either need to submit a form, or use AJAX to call another PHP file and send data to it. 1 Link to comment Share on other sites More sharing options...
niche Posted May 21, 2012 Author Share Posted May 21, 2012 Added the quotes and moved the JS to the end of the script. I suppose that demos how JS executes. I didn't think the JS executed until the onclick, but apparently it does right? Link to comment Share on other sites More sharing options...
Ingolme Posted May 21, 2012 Share Posted May 21, 2012 The example I gave is just some arbitrary Javascript code. PHP doesn't care what Javascript does. Maybe you can understand the relation between PHP and Javascript better like this (which is equivalent to your first script: <?phpecho '<html><head><script type="text/javascript">function displayText() {'; echo 'var hello = ' . $php_var . "\r\n"; echo 'alert(hello);}</script></head><body>'; $php_var = "Hello World!"; echo '<button onclick="displayText()">Display</button></body></html>'; ?> 1 Link to comment Share on other sites More sharing options...
niche Posted May 21, 2012 Author Share Posted May 21, 2012 I think I understand except I wasn't able to get the function to work, with it in the head. I had to move it to the end of the script. Is that why the alert doesn't activate from the previous post? Link to comment Share on other sites More sharing options...
eTianbun Posted May 21, 2012 Share Posted May 21, 2012 Like ingolme said: Echoing a variable before giving any data to it Not putting quotation marks around a string in Javascript. 1 Link to comment Share on other sites More sharing options...
niche Posted May 21, 2012 Author Share Posted May 21, 2012 "I see", said the blind man. That's a nice satisfying conclusion to a light informative topic. Thank you So Called, Ingolme, and CodeName. Link to comment Share on other sites More sharing options...
Guest So Called Posted May 21, 2012 Share Posted May 21, 2012 I'm pleased that you picked up your hammer and saw. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now