0

I have PHP code that tries to output JavaScript and I do something like this:

trailhead_name = <?php echo $objkey->trailhead_name ?> + "";

And I get the unexpected identifier error in my JS.

3
  • Try running your js through jslint.com, it will often give a more specific error. Commented Jun 6, 2011 at 16:43
  • Don't show us what you do "something like", show us what you do. Commented Jun 6, 2011 at 16:44
  • 2
    Don't show us some PHP that generates some JavaScript which throws an error, show us the JavaScript. Commented Jun 6, 2011 at 16:44

2 Answers 2

3

If trailhead_name is a string, you need to put quotes around it (and properly escape anything within it that may not be a valid JavaScript string — like a quote!).

PHP's built-in JSON encoder can do that for you:

trailhead_name = <?php echo json_encode($objkey->trailhead_name) ?>;

Again, that assumes that trailhead_name is a string.

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

Comments

1

Use json_encode:

var trailhead_name = <?php echo json_encode($objkey->trailhead_name); ?>;

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.