uddipana
uddipana

Reputation: 188

keeping multiple checkboxes checked in foreach loop in php

I have multiple checkboxes within a foreach loop and I need to keep the selected checkboxes checked after the form submission. The code is as below. Please help.

     <? $i=0;
        while ($row=mysql_fetch_array($result,MYSQL_ASSOC))
        {
          foreach($row=$val)
          {
            $id="chkbox".$i;
      ?>
          <input type="checkbox" name="chkbx" onclick ="func()" id="<?echo   $id;">? value="<?echo $val \n;?>" <? echo "$val";?>

Now where and how to include the checked property of the boxes..

Upvotes: 0

Views: 1642

Answers (1)

Meenesh Jain
Meenesh Jain

Reputation: 2528

You don't need foreach loop here

This can be done for checking multiple checkbox checked

  <?php
    $i=0;
    while ($row=mysql_fetch_array($result,MYSQL_ASSOC))
    {
        $checked = "";
        if($row['database_column_name']=$val){
            $checked = "checked";
        }
        echo '
         <input type="checkbox" name="chkbx" onclick ="func()" id="'.$id.'" value="'.$val.'" '.$checked.'>'.
          $val
         .'
        ';
    }
    ?>       

Works for me.

Upvotes: 1

Related Questions