0

Following is my code, am getting Uncaught SyntaxError: Unexpected token }, but i don't see any } in my code. window.open is expecting url in quotes, I tried different combinations of single and double quotes but not working and unable to escape the double quote in echo either.Please help

Thanks..

<?php
$a = "https://www.google.co.in/";

?>

<html>
<body>
<form>

<input type="button" width="100" onClick="window.open(<?php echo '"'; echo $a; echo '"'; ?>)" height="100%" value="Edit Record"/>

</form>
</body>
</html>
1
  • To determine where the problem is, look at your JavaScript and HTML, not at your PHP. Commented Oct 24, 2014 at 7:21

2 Answers 2

1

You are outputting " characters into your onClick attribute value. Since you use those characters to delimit the value, the first one ends the script in the middle of the statement.

Use &quot; instead.


But that's a quick and dirty hack. There are better approaches.

  • Do not try to generate JavaScript strings by mashing PHP strings together. Use a robust escaping function. json_encode will give you the JavaScript literal (including quote characters where needed) for any simple data structure.
  • Do not try to generate HTML by mashing strings together. Use a robust escaping function. htmlspecialchars will do all you need.

Such:

onClick="window.open(<?php echo htmlspecialchars(json_encode($a)); ?>)"

But don't use JavaScript when HTML will do:

<a href="<?php echo htmlspecialchars($a); ?>" target="_blank">
Sign up to request clarification or add additional context in comments.

Comments

0

You should use echo "'$a'". The main problem is that you would habe double-double quotes in your onclick attribute. Or even better window.open('<?php echo $a; ?>').

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.