0
Hello, I’ve tried everything with javascript, tutorials I found on the internet, but nothing helps me, I think it’s because my form is dynamic, ie the user who registers the questions and alternatives. In this case, this code takes from the bank two tables, one with the question (it saves, whether the question is mandatory or not) and the other, saves the alternatives (title and id). The problem is how do I make checkboxers mandatory? type, only those in the bank are with the mandatory = 1, and also, all alternatives, have to be with dynamic ids, as it shows the code, someone who knows JS and PHP can give a light? Thank you!
Edit: If the question is required, the user has to select at least one field, otherwise it is not required, do not need to select anything.
<form action="envia.php" method="post" class="ac-custom ac-checkbox ac-cross" autocomplete="off">
<?php
$i = 1;
$i2 = 1;
$sql2 = "SELECT * FROM perguntas WHERE type = '3' ";
$rs2 = mysqli_query($conn, $sql2);
while($dados2 = mysqli_fetch_array($rs2)){
?>
<section>
<h2 style="font-size:3.0em"><?php echo $dados2['title']?></h2>
<?php
if(!empty($dados2['imagem'])){
echo '<img src="adm/perguntas/'.$dados2['imagem'].'" width="100%" />';
};
?>
<div class="ac-custom" >
<ul>
<?php
$sql3 = "SELECT * FROM alternativas WHERE id_de = ".$dados2['id']." ";
$rs3 = mysqli_query($conn, $sql3);
while($dados3 = mysqli_fetch_array($rs3)){
?>
<li>
<input id="cb<?php echo $i++?>" value="<?php echo $dados3['alternativa']?>" name="cb<?php echo $dados3['id']?>" type="checkbox" required>
<label for="cb<?php echo $i2++?>" style="font-size:2.0em"><?php echo $dados3['alternativa']?></label>
</li>
<?php
}
?>
</ul>
</div>
</section>
<?php
}
?>
<div class="ac-custom" style="text-align:center">
<button type="submit" class="btn btn-primary">Enviar</button>
</div>
</form>
What did you mean
tem que ser com ids dinâmicos
? It would be something likecb + a id da alternativa na tabela
?– NoobSaibot