Jump to content

[SOLVED] Addslashes not adding slash


deansatch

Recommended Posts

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?

Link to comment
Share on other sites

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???)

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.