How can I get only values of the form fields that have marked checkbox?
Form:
<form id="form" name="form" method="post" action="">
<input type="text" name="textfield[]" value="textf 1"/>
<textarea name="textarea[]" cols="45" rows="5">some text 1</textarea>
<input name="check[]" type="checkbox" value="checkb 1" />
<input type="text" name="textfield[]" value="textf 2" />
<textarea name="textarea[]" cols="45" rows="5">some text 2</textarea>
<input name="check[]" type="checkbox" value="checkb 2" />
<input type="text" name="textfield[]" value="textf 3"/>
<textarea name="textarea[]" cols="45" rows="5">some text 3</textarea>
<input name="check[]" type="checkbox" value="checkb 3" />
</form>
I want to get form field values for specific textfield and textarea. For example if I mark the first and last checkbox in this example. How can I do that using PHP?
Then result should be:
textf 1
some text 1
checkb 1
textf 3
some text 3
checkb 3
Give your checkboxes and textareas set indexes so you know easily which checkbox corresponds to what form.
<input type="text" name="textfield[0]" value="textf 1"/>
<textarea name="textarea" id="textarea[0]" cols="45" rows="5">some text 1</textarea>
<input name="check[0]" type="checkbox" value="checkb 1" />
<input type="text" name="textfield[1]" value="textf 2" />
<textarea name="textarea" id="textarea[1]" cols="45" rows="5">some text 2</textarea>
<input name="check[1]" type="checkbox" value="checkb 2" />
Then you can quickly check whether a specific checkbox is set, and do something with the associated textarea.
foreach ($_POST['textfield'] as $idx => $value) {
echo $_POST['textfield'][$idx] . "<br />";
echo $_POST['textarea'][$idx] . "<br />";
}
That will only print the textareas that have checked textfields, since those that weren't checked aren't in $_POST.