All in the <head>

– Ponderings & code by Drew McLellan –

– Live from The Internets since 2003 –

About

PHP Duplicate Names

1 January 2004

Quite often when working with HTML forms, it’s necessary to have multiple fields that share the same name attribute. A good example is check boxes and radio buttons, which are grouped based on their name. When the form is submitted, the data is shipped off to a server-side processor whose responsibility it is to interpret the input.

When working with ASP, any values sharing the same name get compiled together as a comma-delimited list. This isn’t an ideal way to work as it can be difficult to split that list back down and be certain that there weren’t stray commas in the input to throw everything off. PHP has a really neat trick whereby if an HTML field name ends in [] it will automatically turn the results into an array. This means you can cycle through the results with absolute certainty that you have collected the data accurately. It also saves you a couple of lines of code, as the most common destination for this sort of data is an array.

The problem arises in that the square brackets PHP uses are not valid characters for use in field names. Whilst a quick test suggests they behave fine in the up-to-date versions of IE and Moz I have running on my desktop, as the characters are out-of-spec they must be consider unsafe, if not harmful, for use. Unfortunately, this leaves me in a sticky situation – how do I deal with multiple fields using the same name? There must be a solution, but I can’t seem to find it.

- Drew McLellan

Comments

  1. § qid: I don’t know whether it’s in the spec, but IS valid, according to the W3C validator. I’ve tried it in browsers from the latest Mozilla and IE all the way down to Lynx, so I’m pretty sure it’s safe to use. I somehow doubt there are any browsers out there strict enough to care about a detail like this. Keep in mind, there’s what’s in the spec, and then there’s what people actually use (and what browsers actually support).
  2. § Ben Pirt: I’ve been using this technique now for a couple of years and have _never_ had any problems with it.

    It is one of the most useful tricks with PHP that I know, and saves many a headache when cycling through form data.

    Hopefully new browsers won’t become more stringent in their support of this standard :-)
  3. § Brent: We are using that on a site now and it took me a while to realize what the problem was. I wish there was a better way to do that so that it would validate.
  4. § Marcus Tucker: Actually, ASP also provides an (pseudo-)array-based means of accessing individual values for a given field of a given request collection.

    For example, Request.Form('SelectedNewsletters').Count will give you the number of values that was sent by the browser for the SelectedNewsletters field, and Request.Form('SelectedNewsletters')(N) will return an individual value, where N is number representing the index of the value (i.e. between 1 and the number returned by .Count)

    So there!! :p
  5. § Marcus Tucker: And it looks you’ve got a bug in your comment script, Drew... the above post looked fine in preview... all my tags were closed correctly as I recall... ;)
  6. § liorean: Not quite. The NMTOKEN, NAME and ID types are not used for the name attribute of form fields - they use CDATA type, which allows pretty much any characters. Remember that form fields are still named using the name attribute for submitting, even if you should use the id attribute for accessing them using the DOM or if you want to style them using CSS.
  7. § liorean: Grr, I had an excerpt from the XHTML 1.0 Strict DTD there, and it showed in the preview, but disappeared in the post. Well, here it is:
    {!ELEMENT input EMPTY}
    {!ATTLIST input
      %attrs;
      %focus;
      type        %InputType;    ”text”
      name        CDATA          #IMPLIED
    [...]
    }

Textile Help

Photographs

Work With Me

edgeofmyseat.com logo

At edgeofmyseat.com we build custom content management systems, ecommerce solutions and develop web apps.

Recent Links

Affiliation

  • Web Standards Project
  • Britpack
  • 24 ways

About Drew McLellan

Photo of Drew McLellan

Drew McLellan has been hacking on the web since around 1996 following an unfortunate incident with a margarine tub. Since then he’s spread himself between both front- and back-end development projects, and now is Director and Senior Web Developer at edgeofmyseat.com in Maidenhead, UK (GEO: 51.5217, -0.7177). Prior to this, Drew was a Web Developer for Yahoo!, and before that primarily worked as a technical lead within design and branding agencies for clients such as Nissan, Goodyear Dunlop, Siemens/Bosch, Cadburys, ICI Dulux and Virgin.net. Somewhere along the way, Drew managed to get himself embroiled with Dreamweaver and was made an early Macromedia Evangelist for that product. This lead to book deals, public appearances, fame, glory, and his eventual downfall.

Picking himself up again, Drew is now a strong advocate for best practises, and stood as Group Lead for The Web Standards Project 2006-08. He has had articles published by A List Apart, Adobe, and O’Reilly Media’s XML.com, mostly due to mistaken identity. Drew is a proponent of the lower-case semantic web, and is currently expending energies in the direction of the microformats movement, with particular interests in making parsers an off-the-shelf commodity and developing simple UI conventions. He writes here at all in the head and, with a little help from his friends, at 24 ways.