Forum Moderators: coopster
var text='<?php print "$item1";?>'
My problem is that when the text in the database stored in $item1 contains an apostrophe, it messes up the javascript "var" assignment. The easiest solution I think is in my text insertion into the database to search for apostrophes in the string and replacing them with the html code for an apostrophe as follows.
$item1 = str_replace("'", "’", $item1);
This doesn't seem to work however. What I need to do is search the string before it is inputed into the MySQL and replace all apostrophes with the text: ’
var content='<font size="-1"><?php while ($row = mysql_fetch_array($result)) {print "• {$row['item1']}<br><br>";}?></font>';
2 Suggestions! , if you dont like em, please discard em.
1) Never accept or deny working of a script before executing it and running results.
Suggestion 2) Keep things simple. its easy to debug em
i would write your code like this
<?php
$content='<font size="-1">';
while ($row = mysql_fetch_array($result))
{
$content=$content."• ".$row['item1']."<br><br>";
}
$content=$content.'</font>';
?>
<script language=javascript>
var content='<? echo addslashes($content)?>';
alert(content);
</script>
This works Perfect! There is no need to bother about the apostros. or var content='';
you can check after executing the above code
your variable [content] will have everything in it either single qoutes or double qoutes, without any error.
That's why they say, Keep it Simple!
Hope this Helps,
Cheers
Kami
I think the solution is before the text is ever stored in the database, to search for apostrophes and replace them with the ’ code. I am certainly an amature at this so please tell me if I am mistaken.
If you wish to implement this solution, you can convert on the way in with htmlentities($yourvariable), then just echo or print your code on the way out... the only draw back is it will convert everything to the html entity. To convert back to actual code you can use html_entity_decode($yourvariable); (I normally make sure I have magic quotes turned off also.)
The other option is to do replace on the ' character with a simple replace:
preg_replace("/'/","’",$yourvariable);
Hope this helps.
Justin