+ 2
How to get php variable to JavaScript variable
How to get php variable to JavaScript variable
6 Antworten
+ 1
You can do this
var js = "<?php echo $var ?>";
+ 12
To pass scalar data held in a PHP variable ( $val ) to a JavaScript variable, place the following in a JavaScript segment: var val = " <?php echo $val ?> "; Notice the quotes around the PHP tags. This will result in a string value in JavaScript which you may need to convert for use in your scripts.
+ 8
The two options are presented by @Toni and @WASP.
@Toni's approach involves dynamically writing the JavaScript on the PHP page during the initial page request. Your static JS can then run based on the initialized value.
@WASP's approach involves loading the PHP generated page on the client, then using AJAX to make a secondary call via Javascript to request the value as JSON or even XML.
Which of these options best fit your situation?
+ 5
If you send the variables as query strings you should be able to get them in JS. It's the fastest way I can think of at the moment.
https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript
+ 3
ok. thanks all
+ 2
Json?