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?

Community
  • 1
  • 1
node ninja
  • 31,796
  • 59
  • 166
  • 254
  • 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. – rjz Mar 05 '12 at 05:56
  • this type of question is all over the web, please google it first – t q Mar 05 '12 at 05:58
  • http://stackoverflow.com/questions/2338942/access-a-javascript-variable-from-php#answers-header – sandeep Mar 05 '12 at 06:09
  • you want JS inside php or php inside JS ? – Sam Arul Raj T Mar 05 '12 at 06:21

3 Answers3

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.

seeming.amusing
  • 1,179
  • 1
  • 8
  • 18
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.

Ryan Kempt
  • 4,200
  • 6
  • 30
  • 41
0

try with it

<script>
    var whatever = "<?php echo $phpVar ?>";
</script>
epascarello
  • 204,599
  • 20
  • 195
  • 236
thecodedeveloper.com
  • 3,220
  • 5
  • 36
  • 67