0

Possible Duplicate:
Pass a PHP string to a Javascript variable (including escaping newlines)
Access a JavaScript variable from PHP

I have a Javascript function that takes in one variable. The function has some php code inside it. I want to use this variable inside the php section of the function. I couldn't get it to work. How is it done?

4
  • 1
    No-can-do, boss, at least not the way I think you're thinking :-) The PHP is being interpreted on the server before it's sent to the client. You can use PHP to populate a javascript variable directly, but going the other direction requires some kind of AJAX post. Commented Mar 5, 2012 at 5:56
  • this type of question is all over the web, please google it first Commented Mar 5, 2012 at 5:58
  • stackoverflow.com/questions/2338942/… Commented Mar 5, 2012 at 6:09
  • you want JS inside php or php inside JS ? Commented Mar 5, 2012 at 6:21

3 Answers 3

1

The issue is that your PHP code is being rendered on the server before being served to the client. I would recommend either converting the PHP code into Javascript code or creating an AJAX call to the PHP function.

Sign up to request clarification or add additional context in comments.

Comments

0

Start reading about AJAX! You will likely need to rewrite some of the code you have written but what you are attempting to accomplish is not really possible otherwise.

2 Comments

How easy is it to do with AJAX?
It is simple once you get used to it, you will definitely need to do some reading and learning but it is fairly simplistic once you have done it a few times.
0

try with it

<script>
    var whatever = "<?php echo $phpVar ?>";
</script>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.