Page 1 of 1

form elements with the same name

Posted: Tue Aug 12, 2003 2:42 pm
by jessevitrone
I have a form with a group of checkboxes, all with the same name. I'm generating the list of checkboxes, so I don't know how many there are.

I also deal with Java, and using servlets I can get the parameter for the form element and it returns me an array, since there was than one element with the same name.

Does PHP do that? I only seem to be getting back the last element when I check $_POST["elementName"]; I would've thought it would give me back an array, like Java does. Am I missing something? Or does PHP restrict you and only return the value from the last element with that name?

Thanks in advance,
Jesse

Posted: Tue Aug 12, 2003 2:46 pm
by nielsene
add a '[]' to the end of the form element's name on the form page. Then on the processing page use it like an array and all should be good.

Posted: Tue Aug 12, 2003 2:55 pm
by JAM
Example:

Code: Select all

<input type="checkbox" name="foo&#1111;]">
 <input type="checkbox" name="foo&#1111;]">
 <input type="checkbox" name="foo&#1111;]">
 <input type="checkbox" name="foo&#1111;]">

JAM

Posted: Tue Aug 12, 2003 2:59 pm
by jessevitrone
Ah.....just what I was looking for. Thanks.

javascript problem with using [] notation

Posted: Wed Aug 13, 2003 7:26 am
by jessevitrone
If my form element name is now "elementName[]", how can I refer to it with Javascript?

When I try document.forms.myform.elementName[], it's a syntax error. I tried getting it with eval("document.forms.myform.elementName[]") too, but that didn't help.

Any suggestions?

Thanks.

Posted: Wed Aug 13, 2003 8:15 am
by JAM

Code: Select all

// short examples...
$elementName&#1111;] = 'foo';
$elementName&#1111;] = 'bar';

echo 'document.forms.myform.' . $elementName&#1111;0]; 
echo 'document.forms.myform.' . $elementName&#1111;1]; 

/* Results:
 document.forms.myform.foo
 document.forms.myform.bar
*/
or something similiar might help you? No java knowledge here, so...

Posted: Wed Aug 13, 2003 8:22 am
by Seth_[php.pl]
JAM wrote:...
No java knowledge here, so...
Sorry I write this but: Java != JavaScript :)

solution

Posted: Wed Aug 13, 2003 8:54 am
by jessevitrone
I was able to do it with this:

Code: Select all

var elements = document.forms.publishForm.elements;

for (var i = 0; i < elements.length; i++) &#123;
    if (elements&#1111;i].type == "checkbox") &#123;
        elements&#1111;i].checked = true;
    &#125;
&#125;
This only works because I never try to refer to the field by name.
It does what I need it to do for now, hopefully I won't run into a case where I have to get it by name.

Posted: Wed Aug 13, 2003 9:04 am
by JAM
Seth_[php.pl] wrote:Sorry I write this but: Java != JavaScript
Yah, yah... but then I had to type 6 more letters, get tired in my hands and so on... ;)