Questions with Dynamic Checkbox with required or not

Asked

Viewed 143 times

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 like cb + a id da alternativa na tabela ?

1 answer

2


Let’s take steps.

First you should check whether the question is mandatory, for that within the while($dados2 = mysqli_fetch_array($rs2)) create an ex variable:

$obrigatorio = "";

Below the created variable, create a condition if which will check whether the question is mandatory, ex:

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

Second, you must 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>

Alter required for <?php echo $obrigatorio; ?>, being like this:

<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 ?>>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.