0

I have a php file (test.php) that has the following content:

<?php
$ar = array('apple', 'orange', 'banana', 'strawberry');
?> 

When I now want to use the $arvariable in a javascript code inside my test.htmlfile (which looks as follows):

<!DOCTYPE html>
 <html>
  <head>
   <meta charset="UTF8">
    <script src="test.php" type="text/javascript"></script>  
  </head>

  <body>
   <script type="text/javascript">
     var ar = <?php echo json_encode($ar) ?>;
     alert(ar)
   </script>
  </body>
 </html>

I keep getting the error: SyntaxError: Unexpected token '<' (in Chrome and Safari). I read through many other examples, where this error was mentioned, however, none of the proposed solutions worked for me. Could anyone help me here?

user56591
  • 473
  • 1
  • 4
  • 11
  • 2
    If you're seeing that PHP code *in your browser* then that means the PHP isn't executing at all. (Probably because it's an `.html` file.) Additionally, unless you include `test.php` *in your PHP code*, your second page isn't going to have `$ar` defined anyway. – David Oct 17 '15 at 00:57
  • 1
    you can't put php code in a html file – Bak Oct 17 '15 at 00:59
  • yep, was a permission thing. thanks for helping me! – user56591 Oct 17 '15 at 02:08
  • You can't use `` inside `.html` file. – Kausha Mehta Oct 17 '15 at 04:42

0 Answers0