Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > HTML > Elements with the same name

Reply
Thread Tools

Elements with the same name

 
 
Leszek
Guest
Posts: n/a
 
      02-07-2006
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


 
Reply With Quote
 
 
 
 
Erwin Moller
Guest
Posts: n/a
 
      02-07-2006
Leszek wrote:

> 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
 
Reply With Quote
 
 
 
 
Stanch
Guest
Posts: n/a
 
      02-07-2006
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.

 
Reply With Quote
 
Harlan Messinger
Guest
Posts: n/a
 
      02-07-2006
Leszek wrote:
> 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.

> 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?

 
Reply With Quote
 
Jonathan N. Little
Guest
Posts: n/a
 
      02-07-2006
Erwin Moller wrote:
> Leszek wrote:
>
>
>>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)


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>

--
Take care,

Jonathan
-------------------
LITTLE WORKS STUDIO
http://www.LittleWorksStudio.com
 
Reply With Quote
 
Jonathan N. Little
Guest
Posts: n/a
 
      02-07-2006
Stanch wrote:

> 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...

--
Take care,

Jonathan
-------------------
LITTLE WORKS STUDIO
http://www.LittleWorksStudio.com
 
Reply With Quote
 
Jukka K. Korpela
Guest
Posts: n/a
 
      02-07-2006
"Jonathan N. Little" <(E-Mail Removed)> wrote:

>>> <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)

>
> 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).

--
Yucca, http://www.cs.tut.fi/~jkorpela/
Pages about Web authoring: http://www.cs.tut.fi/~jkorpela/www.html


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
xml.sax parsing elements with the same name amadain Python 5 02-09-2010 04:40 AM
Multiple select elements with same name Sam Kong Javascript 15 05-19-2007 03:22 PM
some elements with same name but different attributtes Freeter XML 3 01-03-2007 07:39 AM
Selecting all elements with same class name eggie5@gmail.com Javascript 7 12-09-2006 11:32 AM
User Controls with same name on same page rgparkins@hotmail.com ASP .Net 3 05-16-2006 12:55 PM



Advertisments