0

in this code I pass a PHP var to javascrip to display it in a div element. When the string in less than 50 char it works nice but when it is a little bit longen it crashes. here is my code:

$sql3 = "SELECT * FROM $table2 WHERE id = '$newsId'";
$result3 = mysql_query($sql3);
$thisNews = mysql_fetch_array($result3);
?>
newsContent = "<? echo $thisNews['news']; ?>";

newsDiv  = "<div class='newsBox'>";
newsDiv += "<div class='newsTitle'><? echo $rowArray[$i]['title']; ?></div>";
newsDiv += "<div class='newsBody'>"+newsContent+"</div>";
newsDiv += "<div class='newsFoot'>autor: <? echo $thisNews['author']; ?> - <? echo $thisNews['site']; ?> - <? echo $thisNews['source']; ?></div>";
box.append(newsDiv);
5
  • 2
    Which error when it crashes ? Commented Sep 2, 2012 at 14:26
  • Are there possibly linefeeds or special characters, quotes etc in the db output? Commented Sep 2, 2012 at 14:27
  • There was indeed a double quote in the 51th char Commented Sep 2, 2012 at 14:41
  • Sounds like you should add that as an answer @lanzz Commented Sep 2, 2012 at 14:41
  • Can't you provide whole code to find what's wrong with it? Commented Sep 2, 2012 at 15:09

1 Answer 1

1

Better approach would be newsContent = <?php echo json_encode($thisNews['news']); ?>; — that would take care of encoding any special characters in the string, like quote marks that would terminate your JS string and drop you into code context for the remaining of the string, which is not a good thing.

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

1 Comment

And the especially nasty string </script>.

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.