I have several dynamic checkboxes and I need to know which ones have been clicked so that I can then send that data to the database. I have googled but the information that appears is about "fixed" checkboxes, if someone can help me I would greatly appreciate it. Here is the code of how I make the dynamic checkboxes
include "models/conexion.php";
$dep_mant_usuarios = $_POST['division'];
$sql_modulo = "SELECT idmodulos, idDivision, nombreModulo FROM modulos WHERE idDivision = '$dep_mant_usuarios' ORDER BY nombreModulo ASC";
$query_modulo = $pdo->prepare($sql_modulo);
$query_modulo->execute();
$result = $query_modulo->fetchAll();
$query_modulo = null;
echo'<table class="tg">';
echo'<tr>';
echo'<th class="tg-xldj modulo">MÓDULOS</th>';
echo'<th class="tg-c3ow rol">Ingresar</th>';
echo'<th class="tg-c3ow rol">Modificar</th>';
echo'<th class="tg-c3ow rol">Consultar</th>';
echo'<th class="tg-c3ow rol">Imprimir</th>';
echo'<th class="tg-c3ow rol">Exportar</th>';
echo'</tr>';
if (is_array($result)){
foreach ($result as $row){
$nom_modulo = $row['nombreModulo'];
$id_modulo = $row['idmodulos'];
echo '<tr>';
echo '<td class="tg-hmp3"><input name="" type="checkbox" value="'.$id_modulo.'"> '.$nom_modulo.'</td>';
echo '<td class="tg-0lax" ><center><input name="" value="" type="checkbox"></center></td>';
echo '<td class="tg-0lax" ><center><input name="" value="" type="checkbox"></center></td>';
echo '<td class="tg-0lax" ><center><input name="" value="" type="checkbox"></center></td>';
echo '<td class="tg-0lax" ><center><input name="" value="" type="checkbox"></center></td>';
echo '<td class="tg-0lax" ><center><input name="" value="" type="checkbox"></center></td>';
echo '</tr>';
}
}
echo'</table>';
When you create your checkbox, add the value to the name attribute
idModulo[]
. So it will be an array that stores the values of the checkboxes that are sent by POSTyou retrieve it from POST with
then in the code you use a for or a foreach to check them as any array