Checkbox checked from mysqli $row

Viewed 81
while ($row = mysqli_fetch_array($result)){
   if(row[0]=='Not Approve'){
     echo "<input type='checkbox' name='check_approval[]' value='approve'>";}
     }
     if(isset($_POST['check_approval']){echo "checked";}

Hi guys, above is a part of simplified code in my system.
How do i get the 2nd or 3rd checkbox checked.
In this case, if user checked, it will 'checked' everything.
Is there any similar kind of code in php like this -> (check_approval[1]) to get 2nd row to checked.?

From the above example, output will be like [] [] [] [] [] [] []...

1 Answers

This answer gives support for 2 issues, the first is that OP had the same name field for each iteration in the while loop. user clicks the checkbox all the boxes will be checked. SOLUTION make the checkboxes have a unique name. the second is making the checkbox be checked after a search in SQL, having the elseif statement and echo one of 2 outputs one been not checked and one been checked.

Original code

while ($row = mysqli_fetch_array($result)){
if(row[0]=='Not Approve'){
echo "<input type='checkbox' name='check_approval[]' value='approve'>";}
}
if(isset($_POST['check_approval']){echo "checked";}

to this

$x=0;
while ($row = mysqli_fetch_array($result)){
$x++;
if(row[0]=='Not Approve'){
// condition means to be not checked
echo "<input type='checkbox' name='check_approval".$x."' value='approve'>";
}  
elseif(isset($_POST['check_approval']){
// condition means to be checked
echo "<input type='checkbox' name='check_approval".$x."' value='approve' checked>";
}
Related