I have a php file that contains a javascript object (which is in part echoed by php). That file is called by:
<script src="js/file.php"></script>
The contents of the file are basically:
<?php
header('Content-Type: text/javascript');
$custom_icons = JSON_encode($object);
?>
var JSON_icons = <?php echo $custom_icons ?>;
var icons = JSON_icons + {
"facebook": {'name': 'facebook', 'text': 'Facebook', 'icon_url': 'img/logos/facebook.png', 'url': 'http://www.facebook.com'}
};
Please ignore any coding / syntax errors that might appear due to the shortening of the script. The problem I'm having is that when the script is called, javascript throws the error:
Uncaught SyntaxError: Unexpected token <
Which basically means php didn't go through the file and print what it should. It basically left the file untouched and the php code remained there.
How can I solve this? I'm really lost here.
Thank you!
UPDATE: PHP actually checked the file, found some errors, and consequentially echoed them using html elements. The invalid '<' token comes from a <br/> printed by php and not from the <?php tag itself, like I (dumbly) thought.
var JSON_icons = '<?php echo $custom_icons ?>';{"a":1, "b":2}so if you put quotes around the object you're making it a string so would need to parse itvar JSON_icons = JSON.parse('<?php echo $custom_icons ?>');which is pointless –error_reportingon. Check the resulting PHP and make sure that's legal JavaScript.