lets say we have this:
echo '<form method="post">
<div class="form-group">
<table class="table table-bordered table-hover table-striped" style="width:auto">
<tr>
<td><label for="array">ARRAY_NAME</label></td>
<td>
<input type="checkbox" name="array[]" value="1" /> option1<br />
<input type="checkbox" name="array[]" value="2" /> option2
</td>
</tr>
<tr>
<td><label for="array2">ARRAY_NAME2</label></td>
<td>
<input type="checkbox" name="array2[]" value="1" /> option1<br />
<input type="checkbox" name="array2[]" value="2" /> option2
</td>
</tr>
<tr>
<td><label for="array3">ARRAY_NAME3</label></td>
<td>
<input type="checkbox" name="array3[]" value="1" /> option1<br />
<input type="checkbox" name="array3[]" value="2" /> option2
</td>
</tr>
</table>
</div>
<button type="submit" name="submit" class="btn btn-success">Submit</button>
</form>';
I tried to implement this code: <?php echo (isset($_POST['array1[0]']) && $_POST['array1[0]'] == 1) ? "checked='checked'" : "" ?>
but it didn't work! It only works if you have name="array1"
and name="array2"
. this way I'm thinking I can save multiple data in a parent array saved! Like this form[array1[],array2[],array3[]]
.
Can someone give me a solution because I'm stuck! Thanks in advance guys!!
$array2= $_POST['array2']
, then get elements:$array2[0]...
but NOTE THAT the array will have elements only for checked checkboxes, i.e you may have 10 checkbox tags, only 2nd 4th and last one are checked, soarray2
will have only 3 items, not 10, it will be little tricky to use the array to updated checked-status of your checkboxes