Problems with apostrophe in form text fields

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
edawson003
Forum Contributor
Posts: 133
Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA

Problems with apostrophe in form text fields

Post by edawson003 »

When I enter a text string into text field on my form that includes includes ', when I click submit, the entry in the field is appended to /' on the front end and upon successful post submission /' or //' or ///' ...etc (you get the drift, right?) is inserted into my table. The system seems to add a / everytime an attempt is made to submit the form. "Why more that one form submit attempt?, you may ask", well I am also testing field validation errors for other fields on the form. So, what can I do to (1) prevent the data from being appended with /' and (2) prevent the front end display of the previously entered value from being appended with /'?

I enter "Libby's" into the text field and I get "Libby/'s"

FYI, previous entered values are retained with:

Code: Select all

<input type="text" name="textfield" maxlength="30" value="[color=#0000FF]<?php echo isset($_POST['transfat']) ? $_POST['transfat'] : ''; ?>[/color]" />
Any thoughts on why is this happening anyway?
Attachments
libbys.gif
libbys.gif (4.01 KiB) Viewed 95 times
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Problems with apostrophe in form text fields

Post by jackpf »

Looks like you have magic quotes turned on.
User avatar
edawson003
Forum Contributor
Posts: 133
Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA

Re: Problems with apostrophe in form text fields

Post by edawson003 »

Awesome. Thanks for the clue. I googled it and found a lot of posts about this issue. Solutions provided ranged from contacting your host to editing some php.ini file. Found this on the bottom of someone string:

Code: Select all

 
<?php
if (get_magic_quotes_gpc()) {
        $in = array(&$_GET, &$_POST, &$_COOKIE);
        while (list($k,$v) = each($in)) {
                foreach ($v as $key => $val) {
                        if (!is_array($val)) {
                                $in[$k][$key] = stripslashes($val);
                                continue;
                        }
                        $in[] =& $in[$k][$key];
                }
        }
        unset($in);
}
?>
Don't aks me how it works, but it seems to work.
Last edited by edawson003 on Sat Sep 19, 2009 12:19 pm, edited 2 times in total.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Problems with apostrophe in form text fields

Post by jackpf »

Or you could just put

Code: Select all

php_flag magic_quotes_gpc off
in a htaccess file ;)
User avatar
edawson003
Forum Contributor
Posts: 133
Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA

Re: Problems with apostrophe in form text fields

Post by edawson003 »

I tried the .htacess approach and got 500 server error.

Followed the instructions at this link to create the .htacess file:

http://www.javascriptkit.com/howto/htaccess.shtml

Seems like I might have to contact my host if I want to .htaccess approach, unless I zigged when I should of zagged when I set this thing up.

Thanks though for the advise, at least I have one way to fix the issue until I get a handle on this .htaccess thing!
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Problems with apostrophe in form text fields

Post by jackpf »

I copied that straight from my own htaccess file...so it must be something to do with your server.

Do htaccess files normally work?

For example:
if you create a htacces in your root dir with this in:

Code: Select all

Options +FollowSymlinks
RewriteEngine on
 
RewriteRule ^blah$ /index.php
If you go to http://yoursite.com/blah does it show your index.php file?

I'm thinking your server might have disabled htaccess files...or maybe just php flags. I suggest you try and ask them :)
User avatar
edawson003
Forum Contributor
Posts: 133
Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA

Re: Problems with apostrophe in form text fields

Post by edawson003 »

I'll test that. I actually contacted my host and they suggested I create a file named php.ini in the required directory and just add the line :

magic_quotes_gpc = Off

Seemed to work. Magic quotes are now off. But my form submissions with ' in a text value won't submit to my sql table. Now it display just fine but, just won't post. Do I need to apply stripslashes or something.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Problems with apostrophe in form text fields

Post by jackpf »

No, you need to add them. Run all user supplied data used in queries through mysql_real_escape_string().
User avatar
edawson003
Forum Contributor
Posts: 133
Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA

Re: Problems with apostrophe in form text fields

Post by edawson003 »

The mysql_real_escape_string() worked. Thanks!
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Problems with apostrophe in form text fields

Post by jackpf »

;)

No problem.
Post Reply