-3

I'm trying to get some text from a database and show it using Javascript.

Here is my code:

while ($row = mysql_fetch_array($result)) {

    $content = nl2br($row['content']);
    echo "<a id=\"details\" onclick=\"show_details('{$content}');\"></a>";
    echo "<script type=\"text/javascript\" charset=\"utf-8\">";
    echo "function show_details(id, title, content) {
        alert(content);
    }";
    echo "</script>";

Text type is utf8_general_ci and when it contains a new line, Javascript does not work!

Can anybody help me please?

10
  • 1
    Show your rendered javascript. Ie: Show the output that you see in source view in your browser
    – Curtis
    Oct 4, 2012 at 10:00
  • 2
    You shouldn't keep re-declaring the show_details function inside a loop. Oct 4, 2012 at 10:00
  • when you echo <script> tag in browser, it doesn't render that Oct 4, 2012 at 10:00
  • 6
    Worst question title ever.
    – Aesthete
    Oct 4, 2012 at 10:01
  • @Curt : Nothing happens when i click on the link, but when the text has no new line, it works... Oct 4, 2012 at 10:02

3 Answers 3

0

define the function once, and obey the parameters. You've just defined id, not content. Content is the 3rd parameter, not the first one.

echo "<script type=\"text/javascript\" charset=\"utf-8\">"; 
echo "function show_details(id, title, content) { 
  alert(content); 
}"; 
echo "</script>"; 
while ($row = mysql_fetch_array($result)) { 

  $content = nl2br($row['content']); 
  echo "<a id=\"details\" onclick=\"show_details('id', 'title', '$content');\"></a>"; 
}
2
  • Sorry! This is a shorter code, in the real one the parameters are correct Oct 4, 2012 at 10:11
  • imho I don't like to use inline js. It always makes a mess in the code. You could put the content into a hidden div into the anchor. than create a handler $('a').click(function(){...}) and receive the content from div content = $(this).find('div').html() in the callback function. The content of the callack could be the content of fce show_detail
    – macino
    Oct 4, 2012 at 10:14
0

I think you'd be better off separating your JavaScript and PHP.

So, in your PHP file (or HTML template), set up your script:

<script type="text/javascript" charset="utf-8">
    function show_details(id, title, content) {
        alert(content);
    }
</script>

Something else I've noticed: why does your show_details function have three parameters (id, title, content) but your initialising function below only has content? Try removing id and title for the time being, to see if you can get content working.

Then your PHP could simply be:

while ($row = mysql_fetch_array($result)) {
    $content = nl2br($row['content']);
    echo '<a id="details" onclick="show_details(\'' . $content . '\');"></a>';
}

Now, in terms of the new line causing problems, have you tried something like this?

JavaScript addition:

function escapeHtml(unsafe) {
  return unsafe
      .replace(/&/g, "&amp;")
      .replace(/</g, "&lt;")
      .replace(/>/g, "&gt;")
      .replace(/"/g, "&quot;")
      .replace(/'/g, "&#039;");
}

PHP:

while ($row = mysql_fetch_array($result)) {
    $content = nl2br($row['content']);
    echo '<a id="details" onclick="show_details(escapeHtml(\'' . $content .'\'));"></a>';
}

Alternatively, as suggested by comments on this post, you could try using htmlentities or htmlspecialchars on your PHP's $content such as this:

while ($row = mysql_fetch_array($result)) {
    $content = htmlentities( nl2br( $row['content']) );
    echo '<a id="details" onclick="show_details(escapeHtml(\'' . $content .'\'));"></a>';
}

I'm not convinced this is an encoding problem, but I could certainly be wrong... :)

1
  • 1
    Isn't your escapeHtml function just like PHP's htmlentities? Why not use that?
    – Barmar
    Oct 4, 2012 at 10:12
0

If the text contains an enter, it will render the javascript like this:

show_details('piece of
content');

This is invalid.

Replace the enters with \n:

while ($row = mysql_fetch_array($result)) 
{
    $content = $row['content'];
    $content = strreplace("\n", "\\n", $content); // 'escape' breaks.
    $content = strreplace("'", "\\'", $content); // escape quotes.

    ?> 
    <a id="details" onclick="show_details('<?=$content?>');"></a>";
    <?
}
?>
<script type="text/javascript" charset="utf-8">";
function show_details(content) {
    alert(content);
}
</script>;

As you can see, I moved the declaration of show_details out of the loop, you will only need it once. I also removed the echo's. You'll loose any HTML syntaxt highlighting and you'll need all the escaping. I think this method is much cleaner.

3
  • Doesn't nl2br get rid of all the newlines?
    – Barmar
    Oct 4, 2012 at 10:10
  • nl2br should make that line show_details('piece of<br />content'); indeed.
    – turbonerd
    Oct 4, 2012 at 10:11
  • Thanks, I overlooked that one. Indeed it does. I removed nl2br, because alerting <br> doesn't make sense. If it was to be inserted in the DOM instead, show_details can always replace \n with <br>.
    – GolezTrol
    Oct 4, 2012 at 10:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.