Skip to main content
Inspiring
May 5, 2010
Question

Multiple Selects

  • May 5, 2010
  • 1 reply
  • 292 views

If I have a form with multiple selects with the same name, how do I select matching values across those selects?

Example:

<tr>

<td>Person 1</td>

<td>

<select name="Elected">
<option value="N" selected="selected">No</option>
<option value="Y">Yes</option>
</select>
</td>
<td>
<select name="Department">
<option value="A" selected="selected">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
</td>
<td>
<select name="Position">
<option value="1" selected="selected">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</td>
</tr>


<tr>

<td>Person 2</td>

<td>

<select name="Elected">
<option value="N" selected="selected">No</option>
<option value="Y">Yes</option>
</select>
</td>
<td>
<select name="Department">
<option value="A" selected="selected">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
</td>
<td>
<select name="Position">
<option value="1" selected="selected">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</td>
</tr>

<tr>

<td>Person 3</td>

<td>

<select name="Elected">
<option value="N" selected="selected">No</option>
<option value="Y">Yes</option>
</select>
</td>
<td>
<select name="Department">
<option value="A" selected="selected">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
</td>
<td>
<select name="Position">
<option value="1" selected="selected">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</td>
</tr>


I want the values (Elected, Deapartment and Position) for each Person.

    This topic has been closed for replies.

    1 reply

    ilssac
    Inspiring
    May 5, 2010

    That layout is not going to work well for you.

    Since you have fields with the same names, the browser is just going to append all the selections into one field.  I.E. if you look at the data sent to the server by the browser you are just going to see something like.

    form.elected "N,Y,N"

    form.Department "A,C,B"

    form.Postion "3,1,2"

    One might asume that the values are in the same order and thus the first list item is Person 1 and so on, but that is not a very safe asumption.

    The usual way to handle this is to make the field names unique for each section.  I.E.

    <select name="Person1_Elect">

    ...

    <select name="Person1_Dept">

    ...

    <select name="Person1_Pos">

    ...

    And so on.

    Then it would be pretty easy to match up the submitted data.