There are a number of tutorials about passing PHP variables in Javascript (for example). There are 3 alternative methods I am aware of:
- Using AJAX: A separate Javascript file is used to load the required data, then loaded into all HTML (or PHP) pages where required
- Hidden form inputs: e.g.
<input type="hidden" id="X" value="<?php echo $X;?>"/>
and then using Javascript to get the value - Injecting Javascript into the returned HTML: e.g.
<script> var X = "<?php echo $X;?>";</script>
But I am concerned that these methods are not really secure (EDIT: what I need is to just hide variables from the source code , more is not possible), and can become confusing when the number of variables to pass is large.
Are there any tips or tricks to pass large numbers of variables , from PHP to Javascript? What methods do major websites (Wikipedia, Google, etc.) use?
EDIT2: for example
imagine I'm going to pass rootpath of my project and some form keys to js
I'm passing rootpath in this case so I can't link everything before it(I can't use external JS)
and it's really Unbecoming to have something like this:
<script>
rootpath = "example/foo";
KEY1 = "example";
KEY2 = "example";
</script>
and also this:
<input type="hidden" id="rootpath" value="example/foo"/>
<input type="hidden" id="KEY1" value="example"/>
<input type="hidden" id="KEY2" value="example"/>
think about a bigger project with lot's of variables to be passed