Reputation: 26281
I have several rows in a table. Each row has two radio buttons which should be only associated with each other, but not be associated with the radio buttons in the other rows. When submitted, my desire is to have given the below a three element array for pk1
, pk2
, and options
. The following will not work as all the options
radio buttons are associated, and thus only one can be selected, and not one per row be selected as I desire. How can I make the radio buttons only be associated on a per row basis.
<table>
<tr>
<td>1<input type="hidden" name="pk1[]" value="1"></td>
<td>1<input type="hidden" name="pk2[]" value="1"></td>
<td>1<input type="hidden" name="pk3[]" value="1"></td>
<td>option1 <input type="radio" name="option[]" value="yes"></td>
<td>option2 <input type="radio" name="option[]" value="no"></td>
</tr>
<tr>
<td>5<input type="hidden" name="pk1[]" value="5"></td>
<td>3<input type="hidden" name="pk2[]" value="3"></td>
<td>1<input type="hidden" name="pk3[]" value="1"></td>
<td>option1 <input type="radio" name="option[]" value="yes"></td>
<td>option2 <input type="radio" name="option[]" value="no"></td>
</tr>
<tr>
<td>1<input type="hidden" name="pk1[]" value="1"></td>
<td>1<input type="hidden" name="pk2[]" value="1"></td>
<td>2<input type="hidden" name="pk3[]" value="2"></td>
<td>option1 <input type="radio" name="option[]" value="yes"></td>
<td>option2 <input type="radio" name="option[]" value="no"></td>
</tr>
</table>
Upvotes: 0
Views: 20
Reputation: 781129
Add an explicit index to the button names.
<table>
<tr>
<td>1<input type="hidden" name="pk1[]" value="1"></td>
<td>1<input type="hidden" name="pk2[]" value="1"></td>
<td>1<input type="hidden" name="pk3[]" value="1"></td>
<td>option1 <input type="radio" name="option[0]" value="yes"></td>
<td>option2 <input type="radio" name="option[0]" value="no"></td>
</tr>
<tr>
<td>5<input type="hidden" name="pk1[]" value="5"></td>
<td>3<input type="hidden" name="pk2[]" value="3"></td>
<td>1<input type="hidden" name="pk3[]" value="1"></td>
<td>option1 <input type="radio" name="option[1]" value="yes"></td>
<td>option2 <input type="radio" name="option[1]" value="no"></td>
</tr>
<tr>
<td>1<input type="hidden" name="pk1[]" value="1"></td>
<td>1<input type="hidden" name="pk2[]" value="1"></td>
<td>2<input type="hidden" name="pk3[]" value="2"></td>
<td>option1 <input type="radio" name="option[2]" value="yes"></td>
<td>option2 <input type="radio" name="option[2]" value="no"></td>
</tr>
</table>
For consistency, you might want to add them as well to the hidden inputs, although they'll get the same indexes automatically.
Upvotes: 2