Forum Moderators: coopster

Message Too Old, No Replies

PHP and Javascript VAR

Using apostrophes from php assigned to javascript var

         

jaandrws

2:39 pm on Jul 25, 2005 (gmt 0)

10+ Year Member



I need to draw text from a database and assign it to a variable using javascript. The line looks like the following.

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("'", "&#146;", $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: &#146;

Anyango

3:56 pm on Jul 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey

Welcome to webmasterworld, this is a commmon problem and the simplest solution is

var text='<?php echo addslashes($item1);?>'

add slashes will take care of your apposts. in $item1 and will send acceptable input to mysql without any errors related to ' or "

Kami

jaandrws

5:05 pm on Jul 25, 2005 (gmt 0)

10+ Year Member



I'm not sure how to integrate your solution into the following line of code. Furthermore, won't your solution place an apostrophe in the html code? This is what is causing me the problem. It is confusing the var = ' '. The line of code cannot contain an actual apostrophe or the value is never assigned to the variable "content."
I think the solution is before the text is ever stored in the database, to search for apostrophes and replace them with the &#146; code. I am certainly an amature at this so please tell me if I am mistaken.

var content='<font size="-1"><?php while ($row = mysql_fetch_array($result)) {print "&bull;&nbsp;{$row['item1']}<br><br>";}?></font>';

Anyango

7:54 pm on Jul 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey

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."&bull;&nbsp;".$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

jd01

9:18 pm on Jul 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think the solution is before the text is ever stored in the database, to search for apostrophes and replace them with the &#146; 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("/'/","&#146;",$yourvariable);

Hope this helps.

Justin

jaandrws

11:18 pm on Jul 25, 2005 (gmt 0)

10+ Year Member



DUDE,

I APPRECIATE IT! You gave the answer to the question being asked. The simple line of code: $item1 = preg_replace("/'/","&#146;",$item1);
solved everything! The database now stores my variable with the number code which is what I needed. THANKS!