Having trouble passing text from MySQL to a Javascript function using PHP

Go To StackoverFlow.com


So here's the problem. I have data in a MySQL DB as text. The data is inserted via mysql_real_escape_string. I have no problem with the data being displayed to the user.

At some point I want to pass this data into a javascript function called foo.

// This is a PHP block of code
// $someText is text retrieved from the database

echo "<img src=someimage.gif onclick=\"foo('{$someText}')\">";

If the data in $someText has line breaks in it like:

Line 1
Line 2
Line 3

The javascript breaks because the html output is

<img src=someimage.gif onclick="foo('line1

So the question is, how can I pass $someText to my javascript foo function while preserving line breaks and carriage returns but not breaking the code?


After using json like this:

echo "<img src=someimage.gif onclick=\"foo($newData)\">";

It is outputting HTML like this:

onclick="foo("line 1<br \/>\r\nline 2");">

Which displays the image followed by \r\nline 2");">

2012-04-03 22:42
by Nate
One solution would be to use json_encode. More infosavinger 2012-04-03 22:46
Duplicate of http://stackoverflow.com/questions/168214/pass-a-php-string-to-a-javascript-variable-including-escaping-newline - Basti 2012-04-03 22:47
do you want the values to be in a single line - Starx 2012-04-03 23:36


json_encode() is the way to go:

$json = json_encode($someText); # this creates valid JS
$safe = HtmlSpecialChars($json); # this allows it to be used in an HTML attribute
echo "<img src=someimage.gif onclick=\"foo($safe)\">";

You can see a demo here: http://codepad.org/TK45YErZ

2012-04-03 22:51
by Cal
I've updated the question after using json. Any suggestions for the problem that is occurring - Nate 2012-04-03 23:22
Updated with the bit you're probably missing - quotes inside quotes need to be html escaped too - Cal 2012-04-03 23:25
That was what I needed Cal. Thank you very much. As soon as I have 15 reputation I will vote this up. Thanks again - Nate 2012-04-03 23:34
One more question if you're still around. Now that it has passed to my script, I'm having the data display in a textarea to the user, however I don't want the data to be shown with
etc, is there a javascript equivalent to json_decode() - Nate 2012-04-03 23:39
it sounds like you're storing the '
' in your database, which is probably not what you want to be doing. you could solve it a few ways, but an easy hack would be to do document.getElementById('my-textarea').innerHTML = the_data; so that the browser decodes the HTML for you - Cal 2012-04-03 23:44
alternatively, you could fix this in PHP before generating the HTML, using str_replace('<br />', "\n", $in); - http://codepad.org/4BBc77B - Cal 2012-04-03 23:46


If I'm not interpreting badly you may do this:

// This is a PHP block of code
// $someText is text retrieved from the database

echo "<img src=someimage.gif onclick=\"foo('{".trim( preg_replace( '/\s+/', ' ',$someText ) )."}')\">";
2012-04-03 22:48
by 0plus1
This will remove any kind of new line and effectively alter $someText - Basti 2012-04-03 22:49


You'll save yourself a lot of headaches by pulling the JavaScript out of the HTML:

<img id="myImage" src="someimage.gif"/>
<script type="text/javascript">
    var str = <?php echo json_encode($json); ?>;
        function() {

Or something similer...

2012-04-03 23:26
by Pete


Only json_encode() is enough to escape the new line

echo "<img src=someimage.gif onclick=\"foo(".json_encode($newData).")\">";
2012-04-03 23:38
by Starx