从表单中获取数组

I have a form that uses a checkbox and it gets all the data from the mysql database. So there could be from 1 to unlimited of the checkboxes.

I need to get each of those that are checked when the form is submitted.

It works if all of them are checked, but if only a few are checked, it will either show none or just one of them.

Here is the code I am using to show the checkboxes with data:

while ($myrow = mysqli_fetch_array($result)){
echo<<<END
<input type="checkbox" id="units" name="unit[$i]" value="$myrow[unit_id]" /> $myrow[unit_id]<br />
END;
$i++;
}//end while

Here is the code I am using when the form is submitted:

$i=0;
while($_POST["unit"][$i] != ""){
$unit = $_POST["unit"][$i];
$unit = $res2->real_escape_string($unit);
echo $unit."<br />";
$i++;
} // end while

I believe the problem is the fact that I have while($_POST["unit"[$1] != "") and if one box is checked and then another one 2 or 3 down is checked, it sees the 2nd one empty and stops.

If that's the case, I need help to figure out how to make it run through them all and print out the ones that were checked. Remember, there could be anywhere from 1 to unlimited checked so it can't be a set number like while($i <= 50)

Thank you!

Checkboxes are only sent when they are checked. With while the loop stops as soon as a checkbox denoted by $_POST['unit'][$i] is not set, so the rest never get evaluated. You should consider using foreach for arrays:

foreach($_POST["unit"] as $unit) {
    echo $unit . "<br />";
}

If the key is important:

foreach($_POST["unit"] as $key => $unit) {
    echo $key . " is " . $unit . "<br />";
}

If $i is not important you should use :

name="unit[]"

and afther retrieve the values in a foreach loop

foreach ( $_REQUEST['unit'] as $unit ) ....