1

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
line2
line3')">

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");">

3
  • 1
    One solution would be to use json_encode. More info Commented Apr 3, 2012 at 22:46
  • 1
    Duplicate of stackoverflow.com/questions/168214/… Commented Apr 3, 2012 at 22:47
  • do you want the values to be in a single line? Commented Apr 3, 2012 at 23:36

4 Answers 4

2

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

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

6 Comments

I've updated the question after using json. Any suggestions for the problem that is occurring?
Updated with the bit you're probably missing - quotes inside quotes need to be html escaped too.
That was what I needed Cal. Thank you very much. As soon as I have 15 reputation I will vote this up. Thanks again!
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 <br /> etc, is there a javascript equivalent to json_decode()?
it sounds like you're storing the '<br>' 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.
|
0

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 ) )."}')\">";

1 Comment

This will remove any kind of new line and effectively alter $someText.
0

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); ?>;
    document.getElementById('myImage').addEventListener(
        'click',
        function() {
            foo(str);
        }
    );
</script>

Or something similer...

Comments

0

Only json_encode() is enough to escape the new line

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.