Aroum Tins
Aroum Tins

Reputation: 3

PHP form arrays not received post

I have a form, but I can not receive the data correctly.

I want to receive the id and the option you chose, how can I do this?

I tried to foreach, but could not.

<input type="hidden" id="Id_field[]" name="Id_field[]" value="1"/>
<input type="checkbox" id="option[]" name="option[]" class="validate[required]" value="yes" />Yes
<input type="checkbox" id="option[]" name="option[]" class="validate[required]" value="no" />No


<input type="hidden" id="Id_field[]" name="Id_field[]" value="2"/>
<input type="checkbox" id="option[]" name="option[]" class="validate[required]" value="yes" />Yes
<input type="checkbox" id="option[]" name="option[]" class="validate[required]" value="no" />No

Receiving correctly:

Id_field = 1 option = yes

Id_field = 2 option = no

Upvotes: 0

Views: 37

Answers (1)

kero
kero

Reputation: 10658

First of all, id's need to be unique, so this isn't valid (but that doesn't matter for the question). What you can do is use the id in the name like this

<input type="checkbox" name="option[1]" class="validate[required]" value="yes" />Yes
<input type="checkbox" name="option[1]" class="validate[required]" value="no" />No

<input type="checkbox" name="option[2]" class="validate[required]" value="yes" />Yes
<input type="checkbox" name="option[2]" class="validate[required]" value="no" />No

Now you will know by the value if yes or no has been clicked and by the key the id. Also you might consider changing to radiobuttons, then one has to chose either one but can't answer both.

Upvotes: 2

Related Questions