form elements with the same name

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jessevitrone
Forum Newbie
Posts: 15
Joined: Tue Aug 12, 2003 2:42 pm

form elements with the same name

Post 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
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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;]">
jessevitrone
Forum Newbie
Posts: 15
Joined: Tue Aug 12, 2003 2:42 pm

JAM

Post by jessevitrone »

Ah.....just what I was looking for. Thanks.
jessevitrone
Forum Newbie
Posts: 15
Joined: Tue Aug 12, 2003 2:42 pm

javascript problem with using [] notation

Post 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.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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...
User avatar
Seth_[php.pl]
Forum Commoner
Posts: 30
Joined: Sun Aug 10, 2003 5:25 am
Location: Warsaw / Poland

Post by Seth_[php.pl] »

JAM wrote:...
No java knowledge here, so...
Sorry I write this but: Java != JavaScript :)
jessevitrone
Forum Newbie
Posts: 15
Joined: Tue Aug 12, 2003 2:42 pm

solution

Post 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.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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... ;)
Post Reply