Elements with the same name

Discussion in 'HTML' started by Leszek, Feb 7, 2006.

  1. Leszek

    Leszek Guest

    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
     
    Leszek, Feb 7, 2006
    #1
    1. Advertising

  2. Leszek

    Erwin Moller Guest

    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
     
    Erwin Moller, Feb 7, 2006
    #2
    1. Advertising

  3. Leszek

    Stanch Guest

    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.
     
    Stanch, Feb 7, 2006
    #3
  4. 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?
     
    Harlan Messinger, Feb 7, 2006
    #4
  5. 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
     
    Jonathan N. Little, Feb 7, 2006
    #5
  6. 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
     
    Jonathan N. Little, Feb 7, 2006
    #6
  7. "Jonathan N. Little" <> 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
     
    Jukka K. Korpela, Feb 7, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    494
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    May 16, 2006
  2. Freeter
    Replies:
    3
    Views:
    413
    Freeter
    Jan 3, 2007
  3. amadain
    Replies:
    5
    Views:
    433
    dontcare
    Feb 9, 2010
  4. Replies:
    7
    Views:
    137
  5. Sam Kong

    Multiple select elements with same name

    Sam Kong, May 11, 2007, in forum: Javascript
    Replies:
    15
    Views:
    226
Loading...

Share This Page