tags:

views:

45

answers:

2

I have a form that can be dynamically duplicated (with JS) so that the user can enter as much data as he wants. This works great for text inputs, because I just leave the name attribute the same (ending with a []) and then when the values are posted, it just returns me an array. Now I just realized that this doesn't work so well for radio buttons, because the names actually need to be unique for each set. But from the data standpoint, each set only returns one value, so retrieving the data from the POST data wouldn't be a problem, it just screws up the functionality of my form. There's no way around this, is there? I'm just screwed and I can't use arrays?

+2  A: 

Yeah. From the perspective of HTTP, both radio buttons and checkbox sets are pretty much the same thing (except that selecting a radio button deselects all others in the group).

You might be able to have a submit handler which takes the inputs from the radio button sets and converts them into a bunch of standard inputs that turn into an array, but this is quite hacky. Just put in some more code on the server to build your own array if that's what you need.

Yuliy
*sigh* Yeah... guess I'll have to do that. I spent hours making everything else work the other way, and then tried adding one little yes/no radio button to my form and the whole thing explodes catastrophically.
Mark
If it's just yes-no: why is it a radio button? Just make it a checkbox
Yuliy
@Yulify: I thought about that, but it still doesn't solve the problem. The browsers don't send the value at all if it isn't checked. Therefore, there's no way of associating which form the checked boxes correspond to.
Mark
Use the value of the checkbox to identify the form.
Yuliy
Eh? How can I do that? The values are exactly the same from form to form?
Mark
So make them different when you're cloning the checkboxes.
Yuliy
That's just as much work but more hacky than the non-array solution!
Mark
+1  A: 

I just solved this same problem.

If you have more than one group of radio buttons, you can still use them in arrays:

<input type="radio" name="radiobutton[0]" value="a"><br>
<input type="radio" name="radiobutton[0]" value="b"><br>
<input type="radio" name="radiobutton[0]" value="c"><br>
<br>
<input type="radio" name="radiobutton[1]" value="x"><br>
<input type="radio" name="radiobutton[1]" value="y"><br>
<input type="radio" name="radiobutton[1]" value="z">

for example.

When you submit that form, and assuming you pick "a" and "x", you will have an array "radiobutton" that looks like

radiobutton[0] = "a";
radiobutton[1] = "x";

It works because each group has a unique name, but still uses the array syntax.

Austin Hyde
Arghh...it's so simple >.< Well.. it's still a pain because you have to modify the array index in the name via JS, but... oh well.
Mark