I have array of HTML data
<tr><input type="checkbox" name="checkbox[]" value="0"> <input type="text" name="char[]" value="A"></tr> <tr><input type="checkbox" name="checkbox[]" value="1"> <input type="text" name="char[]" value="B"></tr> <tr><input type="checkbox" name="checkbox[]" value="2"> <input type="text" name="char[]" value="C"></tr>
How can I get all of of item in PHP Post? I need to get all data and check if the checkbox were check or not. Like so:
<?php
foreach($_POST["char"] as $data) {
//If Checkboxes Checked:
// DO INSERT CHECKED ROW
//Else If Not Checked:
// ECHO VALUE NOT CHECKED ROW
}
?>
If I’m trying, the data were different from what I has check or only the checked value that sent.
Thank you.
Advertisement
Answer
Use indexes in your field names:
<form method="POST">
<table>
<tr>
<td><input type="checkbox" name="checkbox[0]" value="0"></td>
<td><input type="text" name="char[0]" value="A"></td>
</tr>
<tr>
<td><input type="checkbox" name="checkbox[1]" value="0"></td>
<td><input type="text" name="char[1]" value="B"></td>
</tr>
<tr>
<td><input type="checkbox" name="checkbox[2]" value="0"></td>
<td><input type="text" name="char[2]" value="C"></td>
</tr>
</table>
<input type="submit" name="submit">
</form>
Then you can use a loop counter:
for ($i = 0; $i<count($_POST['char'];$i++) {
if (isset($_POST['checkbox'][$i]) {
// Do something if checkbox is set using $_POST['char'][$i]
} else {
// Do something if checkbox is not set
}
}