How to create a form in PHP with checkbox groups and associated select menu for each checkbox

223 views Asked by At

I'm trying to do with PHP and HTML, a form that contains a group of checkboxes and drop-down menus.

I would like that for each chexboxes, there is an associated select menu.

I create checkbox groups dynamically from a MySQL database

For exemple :

Form :

Checkbox 1 (0:off/1:on) - Selectmenu 1 (0:Red, 1:green, 2:blue, 3:yellow)

Checkbox 2 (0:off/1:on) - Selectmenu 2 (0:Red, 1:green, 2:blue, 3:yellow)

Checkbox 3 (0:off/1:on) - Selectmenu 3 (0:Red, 1:green, 2:blue, 3:yellow)

Checkbox 4 (0:off/1:on) - Selectmenu 4 (0:Red, 1:green, 2:blue, 3:yellow)


I managed to create this, and get via POST the checkbox values through an array (input name="check_list[]")

By cons I can not retrieve the values of select menus, and even less to associate them with the corresponding checkbox

Exemple : I would like to get variables like this (via POST):

Checkbox1 and Selectmenu 1 = 0,3  (off and yellow)

Checkbox2 and Selectmenu 2 = 0,2  (off and blue)

Checkbox3 and Selectmenu 3 = 1,1  (off and green)

Checkbox4 and Selectmenu 4 = 0,0  (off and red)

Source code :

<?php

//Creation of the select menu
$sql = '';
$sql = 'SELECT ID, freq FROM z_ac_freq ORDER BY ID ASC';


        $query = mysqli_query($sql_connect, $sql);

            $select_freq = '<div class="select">';
            $select_freq .= '<select type="text" name="id_freq" value="" maxlength="80" class="select-text" required>';         


        while($liste = mysqli_fetch_array($query))
        {
            $select_freq.='<option value="'.$liste['ID'].'">'.$liste['freq'].'</option>';

        }

        $select_freq.='</select>';
        $select_freq.= '<span class="select-highlight"></span>';
        $select_freq.= '<span class="select-bar"></span>';
        $select_freq.= '</div>';

?>

The form

<form method="POST" id="form" action="?action=addctrl" name="actrl">
<div class="ntable">    
<table class="adds" style="text-align: left; width: 100%;" border="0"
 cellpadding="2" cellspacing="2">
  <tbody>
    <tr>              
      <td style="text-align: right; width: 44%"></td>
            <td>
            <?php 
            while ($donnees = mysqli_fetch_array($query)) { 
            ?>  
            <div id="element1"> 
                <div class="toggler">
                    <input id="toggler" name="check_list[]" type="checkbox" data-validation="checkbox_group" data-validation-qty="min1" name="check_list[]" value="<?php echo $donnees['ID'] ?>">
                </div>
            </div>
            <div id="element2"><font color="<?php echo $color ?>"><p>&nbsp;<?php echo $donnees['commentaire']; ?></p></font><?php echo $select_freq ?></div> 
            <br><br>
        <?php } ?>
        </td>
    </tr>
    <tr>
      <td><input type="hidden" name="id_st" value="<?php echo $id_st; ?>"/></td>
    </tr>
    <tr>
      <td><input type="hidden" name="nom" value="<?php echo $nom; ?>"/></td>
    </tr>                  
    <tr>
      <td style="text-align: right;"><br></td>
      <td><input type="hidden"></td>
    </tr>
    <tr>
      <td style="text-align: right;"></td>
      <td><input type="submit" value="Enregistrer">&nbsp;<input type="button" onclick="location.href='?action=stations';" value="Retour" /></td>
    </tr>
  </tbody>
</table>
</div>
</form>

I hope my question is clear enough.

How do I get there?

Thanks

0

There are 0 answers