tags:

views:

58

answers:

3

In my below code when the $artist or $title variables contain " it causes the JavaScript command to break.

Is there another way I can encode these other than addslashes() to fix this?

$artist = addslashes($row['artist']);
 $title = addslashes($row['title']);

 echo '<div class="play" style="display: inline"><a href="javascript:playSong'."('$artist','$title','$row[file]','$row[id]')".'">
A: 

You could try urlencode().

Kaivosukeltaja
+1  A: 

you can consider url encoding for variables, like

$artist = rawurlencode($row['artist']);
$title = rawurlencode($row['title']);

echo "..... playSong(unescape('$artist'), unescape('$title')... ";

or move decoding into playSong function.

// edit: this is how you get your quotes right

echo "<div class='play' style='display: inline'><a href=\"javascript:playSong(unescape('$artist'),unescape('$title'),'$row[file]','$row[id]')\">....";
stereofrog
The problem is that if there is " in the echo ''; it breaks the JavaScript command not it displaying wrong. So I need to encapsulate the variables correctly I guess.
ian
added complete expample to the post
stereofrog
A: 

Try this

echo '<div class="play" style="display: inline"> <a href="javascript:playSong('$artist','$title','$row[file]','$row[id]')" >'

mepo
i formatted your code, because the tags were stripped out. `echo ' '` made me lol
knittl