Page 1 of 2

PHP Implementation of Select Violates HTML Standard

Posted: Sat Apr 03, 2010 1:41 pm
by jcobban
In my opinion the implementation of the passing of values from an HTML SELECT tag with multiple selection enabled is in violation of the HTML standard. I would appreciate a discussion of why it was felt necessary to use this extraordinary syntax.

Specifically the HTML 4.1 standard specifies that the value of a name keyword must follow the conventions for a variable name. This is required in order that the elements within a FORM can be referred to in the DOM by their names. However PHP requires that the programmer signal that the value of a SELECT tag is to be passed as an array, something which is necessary to implement multiple selection mode, by appending [] to the value of the name attribute. A direct consequence of this requirement is that the SELECT tag cannot be referred to using the DOM, for example in JavaScript, using its name. Rather the circumlocution form.elements["selectname[]"] must be used.

I am certain that this design was not arrived at without serious consideration, but for the life of me I do not understand why PHP cannot conform to the HTML standard in this area.

Re: PHP Implementation of Select Violates HTML Standard

Posted: Sat Apr 03, 2010 4:27 pm
by Eran
You must be confusing something else with PHP. PHP has no native implementation of HTML at all, as such. Perhaps you are using some library component or homebrewed code. If you are, you should probably take your question to the appropriate library forum.

Re: PHP Implementation of Select Violates HTML Standard

Posted: Sun Apr 04, 2010 1:30 pm
by jcobban
pytrin wrote:You must be confusing something else with PHP. PHP has no native implementation of HTML at all, as such. Perhaps you are using some library component or homebrewed code. If you are, you should probably take your question to the appropriate library forum.
You may be right that I have misinterpreted exactly where the problem arises. The syntax I am talking about is documented, if in my opinion rather obscurely, in the PHP manual. See http://www.php.net/manual/en/language.v ... ternal.php and http://www.php.net/manual/en/faq.html.p ... t-multiple. However this may be describing an aspect of the web server that is understood to not be under PHP's control. I will explain my concern more explicitly:

If you code an HTML multiple select in the conventional form, for example:

Code: Select all

<select name='fruit' multiple='multiple'>
<option value='A'>Apple</option>			    
<option value='B'>Banana</option>			    
<option value='C'>Cherry</option>			    
</select>
and select multiple entries the values are passed to the web server as "...?fruit=A&fruit=B&fruit=C". In the PHP script the value of $_GET['fruit'] is the string "C". The fact that the other values are also present is not visible to the script. However if I code the HTML as:

Code: Select all

<select name='fruit[]' multiple='multiple'>
<option value='A'>Apple</option>			    
<option value='B'>Banana</option>			    
<option value='C'>Cherry</option>			    
</select>
then the multiple values are passed to the web server as "...?fruit[]=A&fruit[]=B&fruit[]=C". In the PHP script the value of $_GET['fruit'] is the array {"A", "B", "C"}.

In my opinion although this technique is well known and works, it is in violation of the HTML standards, and knowingly complicates access to the DOM from programming languages such as JavaScript. I would like to understand the reasoning that led to this decision. As an alternative, for example, the PHP implementation could have been designed to automatically create an array value if multiple values were received, and required the script programmer to use is_array to determine if more than 1 value was present in the input. This solution would have been entirely within the control of the PHP implementors, and would not impinge on the HTML and DOM standards.

Re: PHP Implementation of Select Violates HTML Standard

Posted: Sun Apr 04, 2010 1:41 pm
by Christopher
jcobban wrote:In my opinion although this technique is well known and works,
jcobban wrote:it is in violation of the HTML standards, and knowingly complicates access to the DOM from programming languages such as JavaScript. I would like to understand the reasoning that led to this decision.
I think you answered your own question, whether you like it or not. I believe that other languages have followed PHP's syntax because it is well known and works.

Re: PHP Implementation of Select Violates HTML Standard

Posted: Sun Apr 04, 2010 2:11 pm
by Eran
In my opinion although this technique is well known and works, it is in violation of the HTML standards
You can check it using the w3 validation service, it validates. http://validator.w3.org
the PHP implementation could have been designed to automatically create an array value if multiple values were received
Then you could not overwrite values by adding inputs with the same name. That is the common method to send a default value for a checkbox input (since an unchecked checkbox does not send any value).
and knowingly complicates access to the DOM from programming languages such as JavaScript
What complications would those be? the PHP manual even gives an example in one of the links you posted. It's very straightforward

Re: PHP Implementation of Select Violates HTML Standard

Posted: Sun Apr 04, 2010 2:43 pm
by jcobban
pytrin wrote: You can check it using the w3 validation service, it validates. http://validator.w3.org
From this I can only conclude that W3 has coded their validator to explicitly accept the PHP demanded syntax even though it plainly violates the HTML standard because PHP is a fact of the marketplace. In particular section "6.2 SGML basic types" of the HTML 4.01 standard specifies "NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".")." Admittedly the standard does not explicitly outlaw multiple elements having the same name, but the only two cases where it explicitly condones it are for checkboxes and radioboxes.
pytrin wrote: Then you could not overwrite values by adding inputs with the same name. That is the common method to send a default value for a checkbox input (since an unchecked checkbox does not send any value).
That is a good point. When I have found it necessary to implement that sort of functionality I have defined a hidden field with a different name, and used PHP code to look for the different name only if it could not find the principal name. Again if I can avoid violating the HTML standard, or its obvious intent, by a programming workaround, then I prefer to do the workaround.
pytrin wrote:
and knowingly complicates access to the DOM from programming languages such as JavaScript
What complications would those be? the PHP manual even gives an example in one of the links you posted. It's very straightforward
In my opinion having to code 'document.formname.elements["selectname[]"]' is more complicated than coding 'document.formname.selectname'. How can you argue that demanding that someone type 14 additional characters, 6 of them special characters, is not more complicated? I suppose that you might argue that having to code is_array to determine which form of the value had been passed complicates the code, but in my experience when I add support for multiple selection I prefer to cover my ass by coding for both cases, so that the decision of whether or not to implement multiple selection is made by the interface designer, not the web server application developer.

In summary, while you may be comfortable with the current implementation, I am not. I do not appreciate being forced to violate the HTML standard by an avoidable decision by the PHP implementors. If there were even a setting in php.ini that I could use to avoid this I would be satisfied.

Re: PHP Implementation of Select Violates HTML Standard

Posted: Sun Apr 04, 2010 3:45 pm
by Eran
From this I can only conclude that W3 has coded their validator to explicitly accept the PHP demanded syntax even though it plainly violates the HTML standard because PHP is a fact of the marketplace. In particular section "6.2 SGML basic types" of the HTML 4.01 standard specifies "NAME tokens must ...
You could say they ignored it or accepted it, I see the outcome as the same.
How can you argue that demanding that someone type 14 additional characters, 6 of them special characters, is not more complicated?
It is more verbose, but the complexity is exactly the same.
but in my experience when I add support for multiple selection I prefer to cover my ass by coding for both cases, so that the decision of whether or not to implement multiple selection is made by the interface designer, not the web server application developer
If you prefer to always account for both cases, just write a shortcut function that retrieves that element for you. Also, if the verbosity of javascript disturbs you, I'd suggest a Javascript library such as jQuery.
In summary, while you may be comfortable with the current implementation, I am not. I do not appreciate being forced to violate the HTML standard by an avoidable decision by the PHP implementors.
Since it's the first time I ever heard someone complain about this, you might be at a minority here. Regardless, you picked the wrong forum to complain about PHP core decisions, as probably no one here has any particular insight or authority on that. You might want to check the php mailing lists - http://www.php.net/mailing-lists.php

Re: PHP Implementation of Select Violates HTML Standard

Posted: Sun Apr 04, 2010 5:32 pm
by Christopher
jcobban wrote:In summary, while you may be comfortable with the current implementation, I am not. I do not appreciate being forced to violate the HTML standard by an avoidable decision by the PHP implementors. If there were even a setting in php.ini that I could use to avoid this I would be satisfied.
I will go so far as to say you are not only WRONG, but you are the WORST KIND OF WRONG! ;)

You entire argument is that 1) a sentence in the HTML 4 spec references SGML -- which HTML does not even follow strictly and 2) that you know best -- better than the W3C or PHP groups who allow PHP's syntax.

I won't even go into the fact that HTML4 was left behind first by XHTML and now HTML5, which is no longer SGML compliant. Or that HTML itself is a mess of standards and ad hoc agreements based on mainly on how IE and Netscape worked.

All I can say is that you will continue to be not comfortable while tilting at this windmill -- alone. ;)

Re: PHP Implementation of Select Violates HTML Standard

Posted: Sun Apr 04, 2010 7:12 pm
by josh
Its not a violation of the (x)HTML. The standard tells you NOT to rely on name, implied in the following
[snip] .. "#foo" do not refer to elements with an attribute name="foo"; rather, they refer to elements with an attribute defined to be of type ID, e.g., the id attribute in HTML 4. Many existing HTML clients don't support the use of ID-type attributes in this way, so identical values may be supplied for both of these attributes to ensure maximum forward and backward compatibility (e.g., <a id="foo" name="foo">...</a>).
http://www.w3.org/TR/xhtml1/

If you use a modern javascript toolkit, you can usually pull up an element with id foo like this:

Code: Select all

jQuery('#foo').html( 'hello world' ).append('!! Hello stars.');
// <div id="foo">hello world!! hello stars.</div>
I use Xhtml, not html 4. Sounds like html 5 has potential though.

Html 4 is over a decade old. What next, complain that PHP doesn't bundle drivers for your old punch card machine? :drunk:

In this case of multiple valued selects, I'd recommend

Code: Select all

<select id="foo" name="foo[]"

Re: PHP Implementation of Select Violates HTML Standard

Posted: Mon Apr 05, 2010 12:13 pm
by PHPHorizons
Here is the real crux of the problem. Should the so called web standards be followed 100% to the letter or should they simply be seen as guidelines that are optional.

Following web standards 100% means that you can't use the innerHTML property! Wow, now that would complicate things, I'd say.

The truth of the matter is, the last time I checked, both Google's main page and Yahoo's main page failed validation. At the time, they were both the top 2 web sites. I know facebook took number 2, but I haven't checked facebook.

The point is that, on the web, you do what works, not what the standard says to do.


There is a secondary issue here. Validation is seen all too often as being some sort of state of nirvana that must be obtained before one can be proud of their work. I will take as exhibit A and exhibit B: Google and Yahoo respectively, that having a site that validates is neither a trophy to be set on a pedestal nor is it a true validation of your skills as a web developer.

A tertiary issue is that no web standard is implemented 100%. So you can follow a standard, but it may not work in any browser. That would be a real shame... And no browser implements all the same parts of a standard that another browser does. Therefore, one must break from the standard or stay within a small area where most browsers intersect with the most standards.

Cheers

Re: PHP Implementation of Select Violates HTML Standard

Posted: Mon Apr 05, 2010 2:23 pm
by AbraCadaver
As I stated in one of your earlier posts, this is perfectly acceptable. You are confusing a NAME token with a name attribute of type CDATA token.

viewtopic.php?f=1&t=113872&p=597642&hil ... on#p597612

The w3c validator has not been designed to make exceptions to the specifications. Just out of curiosity, why are you so hung up on this?

I participate on php-general@lists.php.net as do a lot of PHP gurus that are also part of PHP internals, etc. If you want even more support for why this does not violate the standards, try there.

Re: PHP Implementation of Select Violates HTML Standard

Posted: Mon Apr 05, 2010 3:55 pm
by jcobban
pytrin wrote: Since it's the first time I ever heard someone complain about this, you might be at a minority here. Regardless, you picked the wrong forum to complain about PHP core decisions, as probably no one here has any particular insight or authority on that. You might want to check the php mailing lists - http://www.php.net/mailing-lists.php
I would not strictly say that I am "complaining" about this. That would be like complaining that the Sun rises in the east. But I understand why the Sun rises in the east, and therefore accept when it shines in my window and wakes me before my alarm clock. I do not understand why PHP has chosen this implementation, so I am unhappy.

[*] I find the syntax offensive from the point of view of an architectural purist.
[*] I don't like being forced to make two changes to the HTML SELECT statement to keep PHP happy, where one is enough for the browser and for Javascript.
[*] I don't like having to use a circumlocution to access a named element in my web page.

I accept that this is the way PHP works, but I feel that if an extra-ordinary solution for multiple-selects was chosen by the original implementers then there must have been an extra-ordinary reason, or reasons, why it was chosen. Having that knowledge would make me a better programmer, because to be a good technician you must thoroughly understand your tools. Aside from programming conventions none of the contributors to this thread have advanced any extra-ordinary reasons for this implementation, nor found inadequacies in my suggested alternative implementations. Furthermore as someone who is notorious for the thoroughness of the documentation that I create for the products on which I have been the chief designer, I am bothered by the incompleteness of the official documentation of this feature. Of course I always have the option of volunteering to improve the documentation.

I have been looking for an appropriate forum for this discussion, but I cannot find any forum on any web-site or within the mailing lists that seems to address architectural issues. I would appreciate any suggestions, because I do not want to waste my time "tilting at windmills".

Re: PHP Implementation of Select Violates HTML Standard

Posted: Mon Apr 05, 2010 6:22 pm
by Weirdan
jcobban, this is the answer to your question:
You are confusing a NAME token with a name attribute of type CDATA token.
"NAME token" refer to the DTD, and in HTML4.01 DTD attributes of type NAME are used only sparingly (for meta name, meta http-equiv and language attributes). 'name' attribute is defined as CDATA CI. Where PHP goes somewhat against the spec is not the set of characters used in name attribute, but the fact that it treats incoming parameters as case sensitive whereas user agents should treat form field names in case-insensitive manner (and thus are allowed to not preserve the case). That said, PHP is not a user agent in this scenario and thus it's not bound by this requirement.

Re: PHP Implementation of Select Violates HTML Standard

Posted: Mon Apr 05, 2010 6:27 pm
by AbraCadaver
Weirdan wrote:jcobban, this is the answer to your question:
You are confusing a NAME token with a name attribute of type CDATA token.
"NAME token" refer to the DTD, and in HTML4.01 DTD attributes of type NAME are used only sparingly (for meta name, meta http-equiv and language attributes). 'name' attribute is defined as CDATA CI. Where PHP goes somewhat against the spec is not the set of characters used in name attribute, but the fact that it treats incoming parameters as case sensitive whereas user agents should treat form field names in case-insensitive manner (and thus are allowed to not preserve the case). That said, PHP is not a user agent in this scenario and thus it's not bound by this requirement.
Are you agreeing with me or disagreeing, I can't tell? I have no idea what the point you're trying to make is other than the CI point, which I didn't address because it wasn't pertinent to the posters topic.

Re: PHP Implementation of Select Violates HTML Standard

Posted: Mon Apr 05, 2010 6:49 pm
by Christopher
jcobban wrote:[*] I find the syntax offensive from the point of view of an architectural purist.
I am an architectural purist and I think the syntax is quite elegant. So it is not the point of view of architectural purist that you present (based on my small survey ;)), but simply your own opinion.
jcobban wrote:[*] I don't like being forced to make two changes to the HTML SELECT statement to keep PHP happy, where one is enough for the browser and for Javascript.
You are not forced to make changes nor is is possible to make PHP "happy". I think you misunderstand the syntax. Within HTML the name is no different than any other. It uses characters for the name. Some of those character happen to be brackets. The brackets simply tell PHP to create the Request variable as an array rather than a scalar. I hope you understand that this syntax works with any Request variable, not just select, radio or checkbox. You can have any combination or all of your GET or POST parameters resolved into arrays by PHP using this powerful syntax.
jcobban wrote:[*] I don't like having to use a circumlocution to access a named element in my web page.
It is not a circumlocution at all. In fact it is the opposite because it is the tersest and clearest way to indicate that the value(s) are to be put into a PHP array.
jcobban wrote:I accept that this is the way PHP works, but I feel that if an extra-ordinary solution for multiple-selects was chosen by the original implementers then there must have been an extra-ordinary reason, or reasons, why it was chosen. Having that knowledge would make me a better programmer, because to be a good technician you must thoroughly understand your tools. Aside from programming conventions none of the contributors to this thread have advanced any extra-ordinary reasons for this implementation, nor found inadequacies in my suggested alternative implementations.
Again, it is only extraordinary in your mind, perhaps because you misunderstand it.

I consider it the most obvious and clear syntax possible. It clearly expresses the intent to have the Request variable be an array instead of a scalar. Using brackets exactly matches the array syntax in PHP (and most other languages). It even follows the empty brackets syntax in PHP to push one or more values onto an array.

In short: understated, clear, obvious, powerful and brilliant.