Apostrophe in html textfield

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
sleepydad
Forum Commoner
Posts: 75
Joined: Thu Feb 21, 2008 2:16 pm

Apostrophe in html textfield

Post by sleepydad »

I'm sure this is old news, but I've spent hours on the web looking for a solution to no avail. I have a site driven by mysql. One of the tables is called 'clients'. Many of these clients have apostrophes in their names. On an admin page that I've created within the site I have a form that dynamically calls in the client name to a textfield for editing. Everything after the apostrophe drops off.

I'm adding to the database using addslashes (and have also tried htmlspecialchars) and stripping slashes for display. If I echo the results outside of the textfield, they're fine. It's just when I try to show them IN the textfield all goes awry.

I'm using:

Code: Select all

 
<?php echo "<input name='name' type='text' size='37' value='".stripslashes($row['name'])."'/>"; ?>
 
Any assistance would be greatly appreciated.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Apostrophe in html textfield

Post by AbraCadaver »

htmlentities() only converts double quotes by default and since you're using single quotes for the delimiter it doesn't work. So here are two ways that work:

Code: Select all

echo "<input name='name' type='text' size='37' value='".htmlentities($row['name'], ENT_QUOTES)."'/>";
//or
echo "<input name='name' type='text' size='37' value=\"{$row['name']}\"/>";
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
sleepydad
Forum Commoner
Posts: 75
Joined: Thu Feb 21, 2008 2:16 pm

Re: Apostrophe in html textfield

Post by sleepydad »

BINGO! Thanks much. I'm back in business.
Post Reply