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 !

4
  • 1
    Possible duplicate of What is the difference between client-side and server-side programming? Commented Mar 9, 2016 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... Commented Mar 9, 2016 at 16:03
  • You can't have a <?php tag inside of a .js file. You can have a <?php tag in a php file with a <script> tag. This just isn't possible. Commented Mar 9, 2016 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. Commented Mar 9, 2016 at 16:04

3 Answers 3

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.
Sign up to request clarification or add additional context in comments.

1 Comment

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 <script src="/common/js/config.js"></script> - it just drops in PHP variables that I want accessible to JS.
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;

1 Comment

Thanks a lot ! I've tried that and I can pass variables and arrays that way.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.