Page 1 of 2

Any way to avoid fieldname[] in HTML code?

Posted: Fri Mar 17, 2006 7:30 pm
by tomprogers
I'm writing a form that has, among other things, a set of identically-named checkboxes. I am used to this being interpreted by the action page as a comma-delimited list of the values from the boxes that were checked.

Code: Select all

<input type="checkbox" name="pid" value="31"> Program #31<br />
<input type="checkbox" name="pid" value="52"> Program #52<br />
<input type="checkbox" name="pid" value="11"> Program #11<br />
If all three were checked, then on the next page pid=31,52,11.

I understand that, by naming the fields pid[], PHP will have access to these values as an array.

What I want to know is if there is any way to preserve identical names on all three fields without putting [] in my HTML - I think it's bad practice to change the HTML because the server-side script doesn't know any better.

Posted: Fri Mar 17, 2006 8:03 pm
by feyd
browsers actually send it like

Code: Select all

pid=31&pid=52&pid=11
php will only see the last because all the others overwrite the prior.

If your php is set to fill the raw post data variable (or the input itself), you might be able to extract it yourself.

http://php.net/ini.core#ini.always-popu ... -post-data

Posted: Fri Mar 17, 2006 8:07 pm
by d3ad1ysp0rk
It kinda makes sense the way it is actually.

Example:

Code: Select all

<?php
$pid = "5";
$pid = "3";
$pid = "6";
echo $pid;

$pid = array("5","3","6");
echo "<pre>" . print_r($pid,true) . "</pre>";
?>
Also, if you try validating code with the id/name being the same for multiple elements, it won't work.

Isn't it just as difficult to put comma delimited variables into seperate variables as looping over an array?

Looping

Posted: Fri Mar 17, 2006 9:59 pm
by tomprogers
It's not a problem of looping; I'll just turn a list into an array. The problem is that a set of checkboxes ought to all have the same name, because they are interchangeable.

Imagine a form that lets you specify which applications a user has access to. You'll have a user id (in this case, their username for matching with LDAP entries), and an internal application ID in the form of an integer. Your form would look like this:

Code: Select all

<input type="checkbox" name="aid" value="1"> Admin Tool<br />
<input type="checkbox" name="aid" value="52"> Calendar<br />
<input type="checkbox" name="aid" value="11"> Work Order
Now, there's no reason these should be named "aid_01", "aid_02", "aid_03", etc. I'd need to loop through all the possible field names to see which are checked, which is foolish. What's more, the "aid_01" scheme is not semantic.

I would very much like access to the query string feyd describes, because then I could simply parse the multiple values out. But it's important that I be able to use method="post". If the raw post data thing will do it, that's a start.

I don't see why this has to be so odd.

Posted: Fri Mar 17, 2006 10:11 pm
by feyd
...and what's wrong with naming them aid[] ? .. they will all show up in $_POST['aid'] which will be an array. All nicely handled for you. I don't see a problem.

Posted: Fri Mar 17, 2006 10:55 pm
by wtf
name[] is not xhtml compliant and in some cases my page doesn't refresh. This is the main reason I've been searching for solution to avoid using [].

Posted: Fri Mar 17, 2006 11:41 pm
by feyd
wtf wrote:name[] is not xhtml compliant and in some cases my page doesn't refresh. This is the main reason I've been searching for solution to avoid using [].
It certainly validates in my tests. And looking at the DTD for XHTML 1.0 Strict and XHTML 1.1, input tags' name attributes are set to CDATA. i.e. arbitrary character data; anything.

Where'd you get this information?

Posted: Sat Mar 18, 2006 1:59 am
by tomprogers
feyd wrote:...and what's wrong with naming them aid[] ? .. they will all show up in $_POST['aid'] which will be an array. All nicely handled for you. I don't see a problem.
It seems awfully PHP-centric. And while there are workarounds for dealing with fields named "aid[]" in JavaScript, and it is XHTML-compliant, I want to adhere to good form design and naming conventions. The way I see it, a given form ought to be able to post to any server-side script without modification - the script is responsible for dealing with the passed data.

Posted: Sat Mar 18, 2006 7:57 am
by d3ad1ysp0rk
But each checkbox is it's own element, they aren't supposed to be grouped, that's the way it was designed. I believe the original intent was single options (aka "Disable BBCode in this post", "Notify me when a reply is posted", etc).
Where exactly does it say it's bad form design to use an array for a set of related checkboxes?

Posted: Sat Mar 18, 2006 8:57 am
by Chris Corbyn
d3ad1ysp0rk wrote:But each checkbox is it's own element, they aren't supposed to be grouped, that's the way it was designed. I believe the original intent was single options (aka "Disable BBCode in this post", "Notify me when a reply is posted", etc).
Where exactly does it say it's bad form design to use an array for a set of related checkboxes?
Indeed, the intention for checkboxes was surely initially a boolean reponse? "Yes" or "No". It's that simple. If we're going to be anal about it the form should use different names altogether for such related items. But that said, it's far more sensible to use an array.

The other options include using JS to disable certain elements (yeah, like that's gonna be a better approach), using JS to combine fields into a delimitted hidden field value (*cough*), using a sequential name (foo_01, foo_01, foo_03).

Clearly the best approach is the use of an array?

Additionally, since you refer to JS... what's wrong with DOM and the use of the ID attribute? :)

Posted: Sat Mar 18, 2006 9:36 am
by feyd
If you want, you could even name them uniquely, but keep them as an automatically generated array in php. How?

aid[0]
aid[1]
...
aid[n]

Frankie goes to Silicon Valley

Posted: Sat Mar 18, 2006 12:55 pm
by tomprogers
d3ad1ysp0rk wrote:But each checkbox is it's own element, they aren't supposed to be grouped, that's the way it was designed. I believe the original intent was single options (aka "Disable BBCode in this post", "Notify me when a reply is posted", etc).
First, let me say that I have not been able to find (in the past 15 minutes) anything that explicity says that a group of checkboxes should all share the same name.

I will, however, point to radio buttons. Obviously, a set must share the same name to achieve the "radio" effect. I think the fact that this effect is bound to the radios' names (as opposed to, say, a group attribute, or all radios within a fieldset or something) speaks to the idea that individual radios are semantically the same as the others, differing only in the value they promise to post. The only way the checkboxes in my example differ from a set of radios is that I want users to have the ability to select more than one (or none) of the options.

Next, I offer the following brief JS function, designed to implement check all/none functionality.

Code: Select all

function setChecks(oField, bState)
{
   i(!oField) return false;
   if(!oField.length) oField.checked = bState;
   else
      for(i = 0; i < oField.length; i++)
         oField[i].checked = bState;
   return true;
}
Note that, in addition to begging the question of proper checkbox naming (and omitting some important validation for brevity), it allows the calling script to act on a distinct set of checks, without relying on a hokey name/pattern-matching scheme paired with a form.elements loop that could behave oddly on forms with similarly named boxes. Also, without modification, it could be used to clear an existing set of radio buttons. And yes, while the script refers to an individual box using array notation, the actual HTML names them all the same.

Assuming we can agree on the above point, I object to using PHP array syntax in my naming scheme because, as I said earlier, I want to avoid coding my forms for a specific language. I work in an environment in which any number of server-side scripts could potentially be asked to process the results of a given form. I'm not saying these other languages can't handle aid[] as a fieldname (because I don't know), but I think the best solution is to mark up my forms in a language-ignorant way. If these languages are genuinely suited to the web, they should each be capable of dealing easily with data generated by the same form, assuming the form itself adheres to the relevent standards.

I've been building forms (for another language) for several years now, and have formed some very strong opinions about how a form ought to be designed. That said, if there is a W3C recommendation, or some other de-facto standard that applies specifically to intelligent form design, I'd be happy to bring my code into line with it. If there is a conversation somewhere dealing specifically with these issues, I'd also be happy to participate. Either way, I'd prefer my HTML practices were not dictated by my choice of processing language, but by HTML standards.

Failing that, I want to be able to do it my way. 8-)

Posted: Sat Mar 18, 2006 1:18 pm
by feyd
It appears you have a bit of work ahead of you creating handlers for each of these languages to all understand how you feel the forms should send information. Have fun.

Posted: Sat Mar 18, 2006 4:03 pm
by wtf
feyd wrote:
Where'd you get this information?
This sample form doesn't validate with webdev extension in Firefox.

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
  <input name="someid[]" type="checkbox" id="someid[]" value="checkbox" />
  <br />
  <input name="someid[]" type="checkbox" id="someid[]" value="checkbox" />
  <br />
  <input name="someid[]" type="checkbox" id="someid[]" value="checkbox" />
  <br />
  <input name="someid[]" type="checkbox" id="someid[]" value="checkbox" />
  <br />
  <input name="someid[]" type="checkbox" id="someid[]" value="checkbox" />
  <br />
  <br />
  <input type="submit" name="Submit" value="Submit" />
  <br />
</form>
</body>
</html>

Posted: Sat Mar 18, 2006 4:17 pm
by John Cartwright
should be name="someid[]" and not id="someid[]"