DEV Community

Galactum
Galactum

Posted on

Uncaught SyntaxError: Unexpected token < in a script tag

So I have a PHP file which contains some JS code and I'm trying to pass an array from PHP into JS

<?php
include('autocomplete.js');
?>
<script type="text/javascript">
// any valid JS code can reproduce this issue because the syntax error is on the line above
var people = <?php echo json_encode($people)?>
</script>

I get an "unexpected token '<'" error and the line referenced is always the one with the script tag in it
Not really sure what to try so if anyone can help out that would be great
Thank you

Top comments (4)

Collapse
 
munaibh profile image
Munaib Hussain • Edited

I'm going to take a punt, hopefully it does the job, but if not sorry! I'd assume you'd want to wrap you include for autocomplete.js in a script tag also? Something like this maybe?



<script>
  <?php include('autocomplete.js'); ?>
</script>
<script type="text/javascript">
  // any valid JS code can reproduce this issue because the syntax error is on the line above
  var people = <?php echo json_encode($people)?>
</script>

Collapse
 
munaibh profile image
Munaib Hussain

This stack overflow question seems to be a similar scenario: stackoverflow.com/questions/367386.... There's probably a lot more wiser nuggets here.

Collapse
 
penandpapers profile image
PenAndPapers

Try to put the include and var into a comment and see what happens next. I'm not even sure if that include line is valid since your referencing a js file instead of php file.

Collapse
 
marcusatlocalhost profile image
Marcus

what does the rendered (echoed) code look like?