admin管理员组

文章数量:1391937

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 !

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 !

Share Improve this question edited Mar 9, 2016 at 16:03 AlexB asked Mar 9, 2016 at 16:01 AlexBAlexB 133 silver badges9 bronze badges 4
  • 1 Possible duplicate of What is the difference between client-side and server-side programming? – KhorneHoly 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... – CD001 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. – Matt Lishman 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. – Gene Commented Mar 9, 2016 at 16:04
Add a ment  | 

3 Answers 3

Reset to default 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 acplish, 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.

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;

.js files are not piled 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.

本文标签: quotUnexpected token ltquot when using ltphp in a Javascript fileStack Overflow