0

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.

6
  • 1
    Try var JSON_icons = '<?php echo $custom_icons ?>'; Commented Dec 28, 2013 at 13:41
  • 4
    What is the actual output of the file.php? Commented Dec 28, 2013 at 13:42
  • @ShankarDamodaran json_encode produces a string to valid object syntax {"a":1, "b":2} so if you put quotes around the object you're making it a string so would need to parse it var JSON_icons = JSON.parse('<?php echo $custom_icons ?>'); which is pointless – Commented Dec 28, 2013 at 13:44
  • 1
    Please show the generated output too. Commented Dec 28, 2013 at 13:46
  • 1
    My guess is the generated PHP has some errors, warnings or notices inside. They are rendered as HTML in the output if you have error_reporting on. Check the resulting PHP and make sure that's legal JavaScript. Commented Dec 28, 2013 at 13:51

2 Answers 2

1

I think the Problem is this:

var icons = JSON_icons + {
    "facebook": {'name': 'facebook', 'text': 'Facebook', 'icon_url': 'img/logos/facebook.png', 'url': 'http://www.facebook.com'}
};

Because JSON_encode($object) results in a string beginning with { and ending with } your result in concatinating ... JSON_icons + { ... results in ... }{ ... which causes the unexpected token error.

Sign up to request clarification or add additional context in comments.

Comments

0

Sorry guys, my bad. PHP does check the file. The invalid token is actually part of an error message printed by php. Here is what the file.php looks like if accessed through the browser:

<br />
<b>Notice</b>:  Undefined variable: icons in <b>C:\...\js\file.php</b> on line <b>42</b><br />

var JSON_icons = null;

...

var icons = custom_icons + {
    "facebook": {'name': 'facebook', 'text': 'Facebook', 'icon_url': 'img/logos/facebook.png', 'url': 'http://www.facebook.com'}, ...
};

P.S: Thanks, @Sbls

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.