I have index.php and I have there some variable in php script. Now I have jqueryfunctions.js file. I would like to send this variable to this js file. I am pretty beginning person, and i am totally new to java script. Could someone tell me how to do this?
Asked
Active
Viewed 62 times
-5
-
give some code which you tried? – Choco Oct 30 '14 at 10:18
-
You cannot set a JS variable to the value of your PHP variable in your external JavaScript file, unless it's being parsed by PHP. You can, however, set a JS variable in a – Ronni Egeriis Persson Oct 30 '14 at 10:20
2 Answers
2
You Can set the JS variable in PHP file.
<script>
var my_val = '<?php echo $php_var; ?>';
</script>

Deepak Rai
- 2,163
- 3
- 21
- 36
-
Note that if the variable is numeric better to omit single quotes for the php tags. – Sampath Liyanage Oct 30 '14 at 10:25
-2
First of all you must aware about server side scripting and client side scripting. PHP is only for server side scripting for process your client side requests. That is you can pass data to javascript only after page load or via ajax.

Lal krishnan S L
- 1,684
- 1
- 16
- 32