Elements with the same name

L

Leszek

Hi.

<div id="id1">
<select name="name">
//options
</select>
</div>

<div id="id2">
<select name="name">
//options
</select>
</div>

Is it ok to have two or more select lists with the same name? (Each in
different div)
Is is possible to check which value from which select was chosen?
For example using php $_POST
or there will be just one value remembered because all select list have the
same name?

Thanks.
Leszek
 
E

Erwin Moller

Leszek said:
Hi.

<div id="id1">
<select name="name">
//options
</select>
</div>

<div id="id2">
<select name="name">
//options
</select>
</div>

Is it ok to have two or more select lists with the same name? (Each in
different div)

No.
The receiving scipt (the one mentioned in the action of the form) will have
trouble telling them appart.
Is is possible to check which value from which select was chosen?

Not if they have the same name!
Why not just name them differently?
For example using php $_POST
or there will be just one value remembered because all select list have
the same name?
indeed.


Thanks.
Leszek


regards,
Erwin Moller
 
S

Stanch

Not sure what you're trying to achieve (javascript issues?), but if
it's of any help, you can do name[] and make it into an array.

e.g. <select name="name[]"></select>

Using name[] you can have multiple selects/inputs with the same form
name.

However, of course it's best if you use distinct names to tell them
apart.

Hope this helps.
 
H

Harlan Messinger

Leszek said:
Hi.

<div id="id1">
<select name="name">
//options
</select>
</div>

<div id="id2">
<select name="name">
//options
</select>
</div>

Is it ok to have two or more select lists with the same name? (Each in
different div)

It's legal. If they're in two different forms, only the one that belongs
to the form being submitted will get sent back to the server anyway. If
they're in the same form, the question becomes: why are you doing this?

What will happen is that two name/value pairs will get sent to the server:

name={value of option selected from list 1}
name={value of option selected from list 2}

What the server will do with this is a different question. I saw someone
else refer to using "name[]". I've only glanced at PHP, but that sounds
familiar: it causes PHP to create an array out of the values, IIRC. In
ASP, though, you don't need the brackets: if more than one name/value
pair having the same name is received, ASP will treat it as an array.
 
J

Jonathan N. Little

Erwin said:
Leszek wrote:

Actually the answer is yes, the data will be in an array

name[0].value has the first form elements value
name[1].value has the second form elements value

<snip>
 
J

Jonathan N. Little

Stanch said:
Not sure what you're trying to achieve (javascript issues?), but if
it's of any help, you can do name[] and make it into an array.

e.g. <select name="name[]"></select>

Using name[] you can have multiple selects/inputs with the same form
name.

Actually the brackets in the name is needed if the receiving script is PHP

<form_element name="some_name[]" ...

but not needed for Perl or JavaScript

<form_element name="some_name" ...
However, of course it's best if you use distinct names to tell them
apart.

Can be very usefully on a form that collects like data but the quantity
is unknown. Say for instance an order form {PHP example}

1st item
<input type="text" name="item[]">
<input type="text" name="price[]">
<input type="text" name="qty[]">
<input type="text" name="amount[]">

2nd item
<input type="text" name="item[]">
<input type="text" name="price[]">
<input type="text" name="qty[]">
<input type="text" name="amount[]">
....

nth item
<input type="text" name="item[]">
<input type="text" name="price[]">
<input type="text" name="qty[]">
<input type="text" name="amount[]">

count the elements in the array, iterate though them and build up an
order...
 
J

Jukka K. Korpela

Jonathan N. Little said:
Actually the answer is yes,

It depends on what you are trying to accomplish.
the data will be in an array

name[0].value has the first form elements value
name[1].value has the second form elements value

No, the data will be sent as name=value items. How the server-side form
handler deals with them is its business. Before making any assumptions,
note that
a) it is unpredictable (browser-dependent) what happens if no choice
is made in a <select> element that has no initially selected options
b) data _should_ appear in the form data in the same order as the
elements appear in HTML source, but it is unwise to rely on this
and, besides, the server-side form handler might reorder the items.

Thus, in practical terms, you should use different names for <select>
elements (if you use them at all).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top