Forum Moderators: coopster

Message Too Old, No Replies

$_POST problem. I think

         

Alger

2:23 am on Sep 28, 2005 (gmt 0)

10+ Year Member



Hey Everyone,

I've got a bit of a problem that I would love some help with.

I am having problems parsing an HTML form that uses an radio buttons with an array for name:

<input type = 'radio' name = '".$link_results['name']."' value = '1'>

The issue seems to arise when $link_results['name'] has spaces in it. I have tried htmlsepecialchars and htmlentities to no avail.

When I reference $_POST[$link_results['name']] on the next page I do get the value of '1' on variables that do not contain spaces in the name. On variables that do contain spaces I get no result.

Anyone know whats up?

grandpa

7:36 am on Sep 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Will this work?

<input type = 'radio' name = '<?php echo $link_results['name'];?>' value = '1'>

tomda

7:58 am on Sep 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't think that space in form control name is valid HTML.
I have checked at W3c and found this
Control names and values are escaped. Space characters are replaced by `+', and then reserved characters are escaped as described in [RFC1738], section 2.2: Non-alphanumeric characters are replaced by `%HH', a percent sign and two hexadecimal digits representing the ASCII code of the character. Line breaks are represented as "CR LF" pairs (i.e., `%0D%0A').
http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4

Last remark: TRY TO AVOID SPACE CHARACTER AS MUCH AS YOU CAN!
Edit:Added last remark ;)

coopster

2:01 pm on Sep 28, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You are correct, tomda, but the user agent (browser) is supposed to handle that for us. <input> "name" attributes are of type CDATA [w3.org] which will indeed allow us to use spaces in the <input> "name" attribute. Good practice? Not really, as tomda stated you can create headaches for yourself. PHP is going to change the spaces to underscores anyway. Hard to find this little diddy in the manual, but the PHP and HTML [php.net] page is a MUST READ for anybody using PHP.


Found under section 2:

Note: Spaces in request variable names are converted to underscores.

I'm with tomda -- keep your name attributes in your forms as simple as possible and you will save yourself from all kinds of trouble.

Alger

4:34 am on Sep 29, 2005 (gmt 0)

10+ Year Member



Figured it out. The for some reason either HTML or PHP was adding _'s to the name. So "photo log" would become "photo_log".

My solution was to use this:

$_POST[str_replace(" ", "_", $results['name'])]

I realize that as far as programming protocol is concerned this isn't the best way to do it, but I am running with a really arcaic database.

Thanks for all your input

Whoops just finished reading your post coopster
thanks