deansatch Posted January 22, 2009 Share Posted January 22, 2009 I have written a script for predictive text using php and ajax. The code in question is this: $li = '<ul>'; while ($row = mysql_fetch_assoc($query)) { $venue =$row['venue']; $li .="<li ><a onclick=\"document.getElementById('venue').value = '".addslashes($venue)."';currentQuery= '".addslashes($venue)."';hideDrop();\" href=\"#\">$venue</a></li>"; } $li .= '<li><a href="#" onclick="hideDrop();">Close</a></li></ul>'; if($venue !=''){echo $li;} } One of the results has an apostrophe in it so $venue = "mary's" Addslashes isn't adding the slash so it is breaking my javascript. If I type echo addslashes("mary's"); it outputs "mary\'s" but it just seems to ignore it when it comes from my db. any ideas? Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted January 22, 2009 Share Posted January 22, 2009 Get rid of them $li .="<li ><a onclick=\"document.getElementById('venue').value = '".str_replace("'","",$venue)."';currentQuery= '".str_replace("'","",$venue)."';hideDrop();\" href=\"#\">$venue</a></li>"; Quote Link to comment Share on other sites More sharing options...
landavia Posted January 22, 2009 Share Posted January 22, 2009 just add above u should try to split the code.. don't make as long as train it hard to read and debug u know Quote Link to comment Share on other sites More sharing options...
deansatch Posted January 22, 2009 Author Share Posted January 22, 2009 fair enough: $li = '<ul>'; while ($row = mysql_fetch_assoc($query)) { $venue =$row['venue']; $li .="<li >addslashes($venue)</li>"; } $li .= '</ul>'; if($venue !=''){echo $li;} } Quote Link to comment Share on other sites More sharing options...
phparray Posted January 22, 2009 Share Posted January 22, 2009 Don't using addslashes. Use htmlentities on the way into your db and you won't have this problem at all. Quote Link to comment Share on other sites More sharing options...
deansatch Posted January 22, 2009 Author Share Posted January 22, 2009 Don't using addslashes. Use htmlentities on the way into your db and you won't have this problem at all. That may be an option, but still doesn't answer my question. Why isn't addslashes working? Quote Link to comment Share on other sites More sharing options...
landavia Posted January 22, 2009 Share Posted January 22, 2009 Don't using addslashes. Use htmlentities on the way into your db and you won't have this problem at all. That may be an option, but still doesn't answer my question. Why isn't addslashes working? i suspect.. PHP version are u use PHP 4 or 5.. and if able the version detail (4.2.3???) Quote Link to comment Share on other sites More sharing options...
deansatch Posted January 22, 2009 Author Share Posted January 22, 2009 PHP Version 5.2.5 Quote Link to comment Share on other sites More sharing options...
landavia Posted January 22, 2009 Share Posted January 22, 2009 PHP Version 5.2.5 that's version have something like auto addslashes how about try addslahes twice *not recomended Quote Link to comment Share on other sites More sharing options...
phparray Posted January 22, 2009 Share Posted January 22, 2009 Here is a sample that works for me. I tested it successfully on php 5.2.6. <input id="venue" name="venue" type="text" /> <?php $wordArray = array('Hope','Hopeing',"Hope's"); $li = '<ul>'; foreach($wordArray as $venue) { $li .= "<li> <a href=\"#\" onclick=\"document.getElementById('venue').value = '".addslashes($venue)."'\"> $venue</a></li>"; } $li .= '</ul>'; echo ($li != '') ? $li : '' ; ?> Since the example is just using and array. If this works for you it means something weird is happening with with db output. Quote Link to comment Share on other sites More sharing options...
deansatch Posted January 22, 2009 Author Share Posted January 22, 2009 I checked the db and it is stored as Mary's Why doesn't it output to my source code as that??? Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted January 22, 2009 Share Posted January 22, 2009 Because it is an html entity and will be converted Quote Link to comment Share on other sites More sharing options...
deansatch Posted January 22, 2009 Author Share Posted January 22, 2009 so if it is converted, why doesn't the slash get added? Quote Link to comment Share on other sites More sharing options...
deansatch Posted January 22, 2009 Author Share Posted January 22, 2009 Sorted! html_entity_decode($row['venue'], ENT_QUOTES, "utf-8" ); Quote Link to comment Share on other sites More sharing options...
phparray Posted January 22, 2009 Share Posted January 22, 2009 so if it is converted, why doesn't the slash get added? Because php is processed on the server and the html entity is not be converted until it gets to the browser. html_entity_decode($row['venue'], ENT_QUOTES, "utf-8" ); works because it does the decode at the server level. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.