Re: form variable name question
| From: | Monte Ohrt | Date: | Thu, 03 Jun 1999 15:08:13 +0000 |
| Subject: | Re: form variable name question | ||
| References: | 1 | Groups: | php.dev |
| Request: | Send a blank email to php-dev+get-6522@lists.php.net to get a copy of this message | ||
Stig Bakken wrote:
>
> On Thu, 3 Jun 1999, Monte Ohrt wrote:
>
> > Hello,
> >
> > This is a question regarding form variables of the same name. According
> > to the documentation, the variable must have "[]" at the end of its name
> > in order for it to be interpreted as an array.
> >
> > example:
> >
> > <INPUT type=hidden name=user_ids value="123">
> > <INPUT type=hidden name=user_ids value="456">
> > <INPUT type=hidden name=user_ids value="789">
> >
> > (returns last value)
> >
> > <INPUT type=hidden name=user_ids[] value="123">
> > <INPUT type=hidden name=user_ids[] value="456">
> > <INPUT type=hidden name=user_ids[] value="789">
> >
> > (returns array of values)
> >
> > This seems to work fine, except when you try using javascript with these
> > variable names. Depending on what you are trying to do, javascript can
> > come up with syntax errors with "[]" in the variable name, and there
> > isn't an easy way around it.
>
> Here's a Javascript workaround (untested code, but you get the idea):
>
> var myElements = new Array();
> for (var i = 0; i < document.myForm.elements.lenght; i++) {
> myElements[document.myForm.elements[i].name] =
> document.myForm.elements[i];
> }
>
> > I'm just wondering, why was the decision made to require "[]" in the
> > variable name to return as an array? Is there an issue where PHP cannot
> > determine the variable type on its own? I don't see a good reason for
> > this limitation. If you want the last value, then the sensible solution
> > would be to grab it off the end of the returned array after the form is
> > submitted.
>
> Do not underestimate "historical reasons". :-)
>
> However, I suppose PHP could detect that you are getting the same variable
> more than once and turn its PHP representation into an array, but I'm not
> sure if this will raise compatibility issues. Controlled by yet another
> configuration directive it would be feasible.
>
> - Stig
A default setting in the .ini file would be a great idea. ;)
Monte