Reputation: 749
I'm using javascript to clone a form according to the int value. I'm using []
brackets for taking array value from other input type.
<input type="text" name="state[]" class="form-control" id="state" required>
and used same for radio input.
<input type="radio" name="gender[]" value="Male">Male
using loop in server side to save those data into db.
for ($i = 0; $i <= $request->limit; $i++) {
$user->name = $request->name[$i];
$user->gender = $request->gender[$i];
$user->address = $request->address[$i];
$user->save();
}
I'm getting error of Undefined offset: 1
on the line:
$user->gender = $request->gender[$i];
as because radio input cannot hold data in array form. When I dump and die request all inputs except radio has data in array form. And value of radio input is succeeded by the value of next radio input of cloned form.
How can I get array value from radio input?
Upvotes: 0
Views: 1202
Reputation: 32335
The problem is that if you all your radio buttons have the name gender[]
, then they are all in the same group, and you can only select one of the options out of all the form clones.
So, for example, if your form looks like this:
<form>
<input type="text" name="name[]" required>
<input type="radio" name="gender[]" value="Male">Male
<input type="radio" name="gender[]" value="Female">Female
<input type="text" name="name[]" required>
<input type="radio" name="gender[]" value="Male">Male
<input type="radio" name="gender[]" value="Female">Female
</form>
If I try to fill it and put "John" in the first name field and then select "Male" from the first two button, then type in "Marry" in the second name field and select "Female" from the last two buttons, the "Male" selection for the first set will disappear - because the browser sees all radio buttons as the same group because they have the same name.
I suggest replacing radio buttons with <select>
boxes.
Upvotes: 0