-2

I have some PHP code inside the script so I need the JavaScript value as a PHP variable, as follows:

<script>
;
 var js_var = 123;
 var php_var = <?php js_var ?>;
 alert(php_var);
;
</script>

Using jQuery to send the value to a PHP page will return the value but not as a PHP variable:

$.get("page.php", {js_var_name: js_var}, function(data) {alert(data)});

I tried also all jQuery AJAX functions: load(), post() and the ajax() method, none of them pass back the value of PHP as above requested.

Is it possible to implement the above with jQuery?

blsn
  • 1,077
  • 3
  • 18
  • 38
  • I don't understand the problem... could you please provide a better example? – Felix Kling Jan 12 '13 at 16:39
  • look in browser source... what shows up for `var php_var =` ? – charlietfl Jan 12 '13 at 16:40
  • 3
    What exactly do you expect `` to do? Try `echo` or `print` and using the `$` variable prefix. – Oldskool Jan 12 '13 at 16:40
  • var js_var represent the value from the pressed radio button (123). I want to store this number, in the same script, as PHP variable (php_var) so that I can use it later in another PHP function. – blsn Jan 12 '13 at 16:48
  • 1
    You seem to misunderstand the client/server architecture this stuff is based on. Please read http://stackoverflow.com/questions/13840429/reference-why-does-the-php-code-in-my-javascript-not-work – bfavaretto Jan 12 '13 at 16:52

2 Answers2

2

Likely you have a syntax issue with :

var php_var = <?php js_var ?>;

Unless you used define() your php variable should start with $ and you need to echo it so that it gets printed to the page

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

If you are assuming that php will read the prior javascript js_var=123 it won't. Javscript is a client side language and php is server side.

charlietfl
  • 170,828
  • 13
  • 121
  • 150
  • I understand that PHP will not read the prior JavaScript js_var=123, but is it possible to store the number 123 as a PHP variable inside JavaScript. – blsn Jan 12 '13 at 16:53
  • are you using `$_GET['js_var_name']` to receive the value from AJAX in php? Really not clear what the issue is – charlietfl Jan 12 '13 at 16:58
  • 1
    @user203952: At the moment when the JavaScript is executed, PHP does not exist anymore... that's why I find you question confusing. You can of course send the value of a variable back to the server and keep in a variable there... is that what you want to do? – Felix Kling Jan 12 '13 at 16:58
  • @ Felix Kling, thank you for this information because I didn’t know that when JavaScript is executed, PHP does not exist anymore. My problem is how do I get back the variable from the server and use it in another PHP function inside the script. – blsn Jan 12 '13 at 17:19
  • use the AJAX you are using to send data to server – charlietfl Jan 12 '13 at 17:29
0

You cannot simply mix PHP and JavaScript. The former is executed on the server, the latter on the client, namely in the user's browser. In order to hand a value from PHP to JavaScript you need to output JavaScript code via PHP or do a AJAX-Request to a PHP-Script, as you tried. When using AJAX, it is the easiest solutions to have PHP script that returns a JSON string. When calling this script using $.get() the result will be stored in a JavaScript variable and you can access all the values from there.

Take a moment and a deep breath...and then start thinking about your scopes again.

Till Helge
  • 9,253
  • 2
  • 40
  • 56