Access PHP var from external javascript file

I can access PHP var using Javascript as follows:

<?php $fruit = "apple"; $color = "red"; ?> <script type="text/javascript"> alert("fruit: " + "<?php echo $fruit; ?>"); // or shortcut "<?= $fruit ?>" </script> 

But what if I want to use an external JS file:

 <script type="text/javascript" src="externaljs.js"></script> 

externaljs.js:

 alert("color: " + "<?php echo $color; ?>"); 
+48
javascript variables php external
May 28 '10 at 12:13
source share
12 answers

You really don't access it, you embed it in javascript code when you serve the page.

However, if your other javascript is not from an external source, you can do something like:

 <?php $color = "Red"; ?> <script type="text/javascript">var color = "<?= $color ?>";</script> <script type="text/javascript" src="file.js"></script> 

and then in the .js file use the color as follows:

 alert("color: " + color); 
+82
May 28 '10 at 12:16
source share

You can also access data from php script in Javascript (I will use jQuery here), like this

Create an input hidden field inside your php file like this

 <input type="hidden" id="myPhpValue" value="<?php echo $myPhpValue ?>" /> 

in your javascript file:

 var myPhpValue = $("#myPhpValue").val(); //From here you can the whaterver you like with you js Value if(myPhpValue != ''){ //Do something here } 

It will also do the job :)

+9
Jun 19 '14 at 15:32
source share

What I saw is to allow .js files through a PHP interpreter. Which I can not recommend.

I recommend retrieving values ​​via AJAX and returning the PHP file to the JS file. This is a much cleaner method.

+7
May 28 '10 at 12:17
source share

First of all, you should understand that no program can have access to another program variable.

When you realize this, the rest is simple.
You can configure the js variable in the main file and then enable your external js or make this external js dynamic generated by PHP also

+4
May 28 '10 at 12:19
source share

What you most likely want is called asynchronous JavaScript and XML (AJAX): http://www.w3schools.com/ajax/default.aspa

Basically, imagine that you can send messages from JavaScript clients to your PHP scripts on the server. In the example you specified (externaljs.js), you will have a script ask the server what $ color is through HTTP. You can also specify a script tag in a PHP script that generates the necessary JavaScript. It depends on what you need to do.

This helps to have some idea of ​​taint validation, data validation and security;)

+4
May 28 '10 at 12:21
source share

As others say, javascript does not have access to php variables. However, it does have access to the DOM. This way you can use php to add attributes to some element of the page. And then you can access these attributes using javascript.

eg. <div id='apple' class='red'> fully accessible for javascript

+3
May 28 '10 at 12:25
source share

externaljs.js is a static file. Of course, he cannot access PHP data. The only way to transfer PHP data to a js file is to physically modify the file by writing it to your PHP script, although this is at best a messy solution.

Change in response to Olafur Vaage: I think writing to the js file is not the only way. Passing js through a PHP interpreter never crossed my mind (for good reason).

+1
May 28 '10 at 12:16
source share
 <script type="text/javascript" src="externaljs.js"></script> 

You can change it to

 <script type="text/javascript" src="externaljs.php"></script> 

And the PHP script could just write JavaScript:

 <?php $fruit = "apple"; echo 'var fruit = '.json_encode($fruit); ... 

Although using AJAX, as Sepehr Lajevardi said, will be much cleaner

+1
May 28 '10 at 12:28
source share

Don's solution is good, besides, if you want to use php array in external javascipt, it can help you:

PHP:

 <?php $my_php_array = []; ?> 

HTML:

 <script type="text/javascript"> var my_js_array = <?php echo json_encode($my_php_array);?> ; </script> <script src = "../public/js/my-external-js.js"></script> 

Javasript: (Now you can use the array as a regular Javascript array)

  my_js_array[0] my_js_array.length 
+1
Jul 22 '16 at 18:56
source share

You cannot do this and are not trying, as this is not recommended, but you can pass php variables as parameters to a function for a function written in external js

0
May 28 '10 at
source share

2017-2018 and above solution:

Since no one has raised it yet, and I think no one thought about combining the base64_encode and json_encode , but you can even send PHP Array variables like this:

index.php

 <?php $string = "hello"; $array = ['hi', 'how', 'are', 'you']; $array = base64_encode(json_encode($array)); 

Then you can simply load the desired js file with the query string parameter as follows:

echo '<script type="text/javascript" src="js/main.php?string='.$string.'&array='.$array.'">';

Then js/main.php will look like this, for example. You can check your variables as follows:

Js / main.php

  <?php if ($_GET['string']) { $a = $_GET['string']; } if ($_GET['array']) { $b = $_GET['array']; } $b = json_decode(base64_decode($b)); echo 'alert("String $a: + '.$a.'");'; echo 'alert("First key of Array $array: + '.$b[0].'");'; exit(); ?> 

When you open index.php following is displayed. So you see, you are not opening js/main.php , and you still have javascript functionality.

enter image description here

0
Oct 06 '17 at 7:40
source share

You can include() them just like anything else:

 <?php $fruit = "apple"; $color = "red"; ?> <script type="text/javascript"> <?php include('/path/to/your/externaljs.js'); ?> </script> 

This will basically display the external file as inline js. The main drawback here is that you are losing the potential benefit of browser caching performance. On the other hand, it is much easier than re-declaring php variables in javascript.

0
Apr 16 '19 at 17:15
source share



All Articles