0

Being new to this, I'm trying to pass a variable from PHP to Javascript.

In my php page, I use a test variable:

    $testval = "x";

In my .js file:

     var exarr = <?php echo json_encode($testval); ?>;

I've tried several things, but it seems I always get Unexpected token < when I include "

What am I doing wrong ?

Thanks !

AlexB
  • 13
  • 2
  • 9
  • 1
    Possible duplicate of [What is the difference between client-side and server-side programming?](http://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – KhorneHoly Mar 09 '16 at 16:03
  • 1
    *What am I doing wrong ?* ... attempting the impossible. Your server won't treat a .js file as PHP and therefore won't invoke the interpreter. You could do it with Ajax calling a .php file or possibly simply rename the file with `.js.php` extension or use `mod_rewrite` to have Apache route that request for the `.js` file to a PHP file... – CD001 Mar 09 '16 at 16:03
  • You can't have a `` tag. This just isn't possible. – Matt Lishman Mar 09 '16 at 16:04
  • You can't pass PHP variables to javascript in this way. Your .js file is run on the client, your php file is run on the server. You need a way to pass the data from the server to the client. Do a google search on AJAX is one way to do it. – Gene Mar 09 '16 at 16:04

3 Answers3

2

In order to use PHP code in any file, the web server has to run that file through the PHP processor. This is configured to happen by default with .php files, but not with .js files.

You could configure your server to process .js files as PHP, but that's generally unwise. At the very least, it creates a lot of unnecessary overhead for those files since most of them won't (or shouldn't) have PHP code.

Without knowing more about the structure of what you're trying to accomplish, it's difficult to advise a "best" approach. Options include (but may not be limited to):

  1. Defining that one var on a PHP page which references the JS file, thereby making it available to the JavaScript code.
  2. Putting the value in a page element somewhere that it can be accessed by JavaScript code, either as a form value or perhaps a data value.
  3. Making an AJAX request to the server to get that value (and other values) after the page has been loaded.
David
  • 208,112
  • 36
  • 198
  • 279
  • 1
    Using `mod_rewrite` to fetch a php file when the js file is requested also works; I've got a *config.js.php* file retrieved through something like `` - it just drops in PHP variables that I want accessible to JS. – CD001 Mar 09 '16 at 16:21
1

.js files are not compiled by PHP. The easiest workaround is to put the Javascript in a <script> block within a .php, but you're making one of the most basic of serverside/clientside mistakes and should rethink your entire approach.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
furtive
  • 1,669
  • 2
  • 13
  • 15
1

If you have that in a js file (like somefile.js) then PHP isn't going to parse that file by default. In the PHP file that links to that JS you can output a script tag and the var you want like:

echo "<script>var exarr = " . json_encode($testval) . "; </script>";

And make sure your script is linked in after that code;

MajicBob
  • 487
  • 2
  • 8