To work around this you need to manually number the checkboxes in your form i.e.
<input type="checkbox" class="switch_1" name="attendo[0]">
<input type="checkbox" class="switch_1" name="attendo[1]">
...
<input type="checkbox" class="switch_1" name="attendo[n]">
This will force the assigned entries to be set (if the checkbox is checked) in $_POST['attendo']
.
use array_values to reindex an array
$array = array_values($array);
I wanted to get an array of all the checkboxes whether selected or not in sequence with the help of the PHP post method. I am getting an array but it shows on selected checkboxes first then the non selected ones. For me, the sequence is very important as this has to be used for Attendance. I am providing the code and output below.
Code:
<form action="aattendscr.php" method="post">
...
<td>
<div class="form-group">
<input type="checkbox" class="switch_1" name="attendo[]">
</div>
</td>
</form>
aattendscr.php
<?php
include "database.php";
if (isset($_POST['submit']))
{
for($x=0;$x<count($data);$x++)
{
if(isset($_POST['attendo'][$x])){echo "On";}else{echo "Off";}
}
}
?>
INPUT
OUTPUT
On On On Off Off
Desired Output
On Off On Off On
Any help will be appreciated.
It is an array storing the of details of student of a particular class. Array ( [0] => on [1] => on [2] => on )