I have a checkbox, and next to each checkbox I have a select option. I have multiple of these, and I want to retrieve the value of the select only if the checkbox is checked for it.
Here is the code:
<form method="POST">
<fieldset>
<input type="checkbox" name="check1[]">
<select name="select1[]">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select
<fieldset>
<input type="checkbox" name="check1[]">
<select name="select1[]">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
</fieldset>
<input type="submit" name="update" value="go">
</form>
Here is my PHP code:
if(isset($_POST['update'])){
for($i = 0; $i < sizeof($_POST['check1']); $i++){
if(isset($_POST['check1'][$i])){
echo $_POST['select1'][$i];
}
}
}
This does not work, if the first checkbox is unchecked then the for loop returns the value of the second select option. Otherwise it works fine. How can i fix this?
Here is the array:
[check1] => Array
(
[0] => on
[1] => on
)
[select1] => Array
(
[0] => 1
[1] => 3
)