tags:

views:

163

answers:

1

I frequently see, in particular in the PHP world, the following writing if you want to create a FORM array.

<input name="MyArray[]" />
<input name="MyArray[]" />

with the square brackets []. Nevertheless, the submit operation just passes the same key entry twice. It appears that the [] is just conventional that maps nicely to the PHP world array, but you would obtain the same result with just the following

<input name="MyArray" />
<input name="MyArray" />

Indeed, in django I get a list of two entries, regardless of the style used.

Is this true ? Are the [] just conventional, or there's actually a real meaning into it from the HTML and HTTP key/value info ?

+3  A: 

They address a limitation of PHP, which doesn't generate an array automatically if multiple values with the same name are submitted, for example from a set of checkboxes or a multiple select. (IIRC it only returns the last value.)

Personally I've always thought it to be a pretty shoddy workaround. Even Classic ASP could cope with that without requiring client-side additions to markup. The server-side platform has no business imposing markup requirements on the client in this way.

NickFitz
I think it was just a design decision. I don't see it as a limitation.
Ionuț G. Stan
It's a limitation if you take existing forms that work perfectly well with any other server-side platform and try to use them with PHP: you have to change the values of all relevant element name attributes, and woe unto you if you miss one. It's an unnecessarily leaky abstraction: the HTML shouldn't be concerned with the fact that the server uses arrays, let alone that it uses C-style array-indexing syntax.
NickFitz
+1 `[]` was an early design mistake and one of those that PHP hasn't got around to fixing since. A different way to access parameters when you wanted to get multiple values would have been better, but wouldn't have fitted with the original `register_globals` strategy.
bobince