Array of selected checkbox is not in sequence using post method

Md. Zakria Sultan :

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

enter image description here

OUTPUT On On On Off Off

Desired Output On Off On Off On

Any help will be appreciated.

Nick :

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'].

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=198586&siteId=1