Questions with Dynamic Checkbox with required or not

0

Hello, I've tried everything with javascript, from tutorials I found on the internet, but nothing helps me, I think it's because my form is dynamic, that is, the user who registers the questions and alternatives. In this case, this code takes two tables from the database, one with the question (in it saved, if the question is mandatory or not) and in the other, saves the alternatives (title and id). The problem is how do I make the checkboxes mandatory? type, only those in the database are required = 1, and also, all alternatives, have to be dynamic ids, as the code shows, someone who manages JS and PHP can give a light? Thank you!

Edit: If the question is mandatory, the user has to select at least one field, otherwise it is not mandatory, you 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>
    
asked by anonymous 22.09.2017 / 19:42

1 answer

2

Let's take steps.

First you should check if the question is mandatory, so within while($dados2 = mysqli_fetch_array($rs2)) create a variable eg:

$obrigatorio = "";

Below the created variable, create a condition if that will check if the question is required, eg:

if ($dados2['obrigatorio']) { // 1 = true
    $obrigatorio = ' required'; // Obrigatório
} else { // 0 = false
    $obrigatorio = ''; // Não é obrigatório
}

Second, you should print the variable $obrigatorio on the screen, then on the line

<input id="cb<?php echo $i++?>" value="<?php echo $dados3['alternativa']?>" name="cb<?php echo $dados3['id']?>" type="checkbox" required>

Change required to <?php echo $obrigatorio; ?> , thus:

<input id="cb<?php echo $i++; ?>" value="<?php echo $dados3['alternativa']; ?>" name="cb<?php echo $dados3['id']; ?>" type="checkbox"<?php echo $obrigatorio; // Retorna se o campo é obrigatório ?>>
    
23.09.2017 / 01:06