Rastur
Rastur

Reputation: 137

Correctly escaping a string in php before passing it to javascript

Ok so i have an input field that generates some text in html form (yea, don't ask). I set it as a php variable and then try to display it in a colorbox pop-up.

My text, as generated:

$text = '<h1>Some title</h1><p>Some text</p>';

And i try to use it like so:

$.colorbox({html:"'.$text.'"});

Obviously the js breaks as i would need to escape my string somehow. Tried htmlspecialchars() and alike but it does not help.

Thanks, and apologies for (i feel) incredibly noobie question.

Upvotes: 1

Views: 58

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798526

JSON is JavaScript literals.

... $.colorbox({html:' . json_encode($text) . '}); ...

Upvotes: 2

Related Questions