Jump to content

php var into js var and back SOLVED with thanks in the final post.


niche
 Share

Recommended Posts

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 by niche
Link to comment
Share on other sites

Guest So Called

How about this?

var hello = <?php echo $php_var; ?>

The other way, probably requires AJAX technique.

Edited by So Called
Link to comment
Share on other sites

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

Guest So Called

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

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.

  • Like 1
Link to comment
Share on other sites

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

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>'; ?>

  • Like 1
Link to comment
Share on other sites

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

"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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...