Validate multiple forms at once - Simulated

Asked

Viewed 103 times

1

I’m creating a simulated site and I’m having trouble correcting it, because I don’t know how to validate numerous forms at once.

What happens:

  • I have questions that are randomly selected by the database;
  • These questions are within a form with their alternatives;
  • I want to correct them (leaving the page or not) by clicking on only one submit - The simulator has 10 questions in all.

Note: I tried to use $_SESSION[] and it worked, but only with one example. I hope you passed my idea, follow a code snippet:

<div class="plataforma_layout_grade" data-reactid="42">
  <div class="plataforma_layout_mestre" data-reactid="58">
    <!--Criação de categoria de simulado-->
    <div class="topico-mestre" data-reactid="59">
      <h1 class="topico-titulo" data-reactid="60"><?php echo"$materia";?></h1>
    </div>
    <div>

      <?php 
        $sql="select * from questoes where id_materia=$disciplina order by rand() limit 10";
        $resultado=mysqli_query($conexao,$sql);

        while ($vetor=mysqli_fetch_row($resultado)):

          $_SESSION["id_questao"] = $vetor[0];
          $id=$vetor[0];
          $pergunta = $vetor[1];
          $imagem = $vetor[2];
          $A = $vetor[3];
          $B = $vetor[4];
          $C = $vetor[5];
          $D = $vetor[6];
          $E = $vetor[7];
          $_SESSION["resposta"] = $vetor[8];
          $resposta=$vetor[8];
      ?>

      <div style="margin-left: 20px;margin-right: 20px; margin-top: 10px;">
        <form method="POST" action="VALIDAR_SIMULADO.PHP" name="codigo"  >

            <fieldset >
              <legend value="<?php echo'$id'; ?>">Cod. <?php echo"$id"; ?></legend>
              <p style="margin-top: -2px;margin-bottom: 8px;"  value="<?php echo'$id'; ?>"> <?php echo"$pergunta"; ?> <br>
              <p><label>A) <input type="radio" name="alternativa" value="A"> <?php echo"$A"; ?></label></p>
              <p><label>B) <input type="radio" name="alternativa" value="B"> <?php echo"$B"; ?></label></p>
              <p><label>C) <input type="radio" name="alternativa" value="C"> <?php echo"$C"; ?></label></p>
              <p><label>D) <input type="radio" name="alternativa" value="D"> <?php echo"$D"; ?></label></p>
              <p style="margin-bottom:-2px;"><label>E) <input type="radio" name="alternativa" value="E"> <?php echo"$E"; ?></label></p>
            </fieldset>
      </div>

      <?php endwhile; ?>

      <input type="submit" name="corrigir" value="Corrigir">
      </form>  
    </div>
  </div>
</div>
  • What should this be p with the property value within the fieldset?

1 answer

1


First, you don’t need multiple forms on your page. Only one can handle it. The only change you need to make to do this is to differentiate the fields between the questions. You can define the value of input as lists, defined the list index. For example:

<form action="validar_simulado.php" method="post">

  <?php $i = 0; ?>

  <?php while($vetor = mysqli_fetch_row($resultado)): ?>

    <?php list($id, $pergunta, $imagem, $a, $b, $c, $d, $e, $resposta) = $vetor; ?>

    <fieldset>
      <legend>Questão <?= $i; ?></legend>
      <p><?= $pergunta; ?></p>
      <input type="hidden" name="id[<?= $i; ?>]" value="<?= $id; ?>">
      <input type="radio" name="alternativa[<?= $i; ?>]" value="a" checked> <?= $a; ?>
      <input type="radio" name="alternativa[<?= $i; ?>]" value="b"> <?= $b; ?>
      <input type="radio" name="alternativa[<?= $i; ?>]" value="c"> <?= $c; ?>
      <input type="radio" name="alternativa[<?= $i; ?>]" value="d"> <?= $d; ?>
      <input type="radio" name="alternativa[<?= $i; ?>]" value="e"> <?= $e; ?>
    </fieldset>

    <?php i++; ?>

  <?php endwhile; ?>

  <input type="submit" name="corrigir" value="Corrigir">
</form>

This code will generate an HTML similar to:

CSS code was just to hand make the result so ugly and Javascript is to run something when pressed the button.

$(() => {

  $("input[type='submit']").on('click', () => {
    console.log("Questão 1: " + $('input[name="alternativa[0]"]:checked').val());
    console.log("Questão 2: " + $('input[name="alternativa[1]"]:checked').val());
  });

});
fieldset {
  margin-bottom: 10px;
  border-radius: 10px;
}

fieldset p {
  font-weight: bold;
}

fieldset legend {
  border: 1px solid black;
  border-radius: 10px;
  padding: 5px;
  box-shadow: 2px 2px grey;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<form action="validar_simulado.php" method="post">
  <fieldset>
    <legend>Questão 1</legend>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit?</p>
    <input type="hidden" name="id[0]" value="5">
    <input type="radio" name="alternativa[0]" value="a" checked> A
    <input type="radio" name="alternativa[0]" value="b"> B
    <input type="radio" name="alternativa[0]" value="c"> C
    <input type="radio" name="alternativa[0]" value="d"> D
    <input type="radio" name="alternativa[0]" value="e"> E
  </fieldset>
  <fieldset>
    <legend>Questão 2</legend>
    <p>Mauris blandit aliquet elit, eget tincidunt nibh pulvinar a?</p>
    <input type="hidden" name="id[1]" value="32">
    <input type="radio" name="alternativa[1]" value="a" checked> A
    <input type="radio" name="alternativa[1]" value="b"> B
    <input type="radio" name="alternativa[1]" value="c"> C
    <input type="radio" name="alternativa[1]" value="d"> D
    <input type="radio" name="alternativa[1]" value="e"> E
  </fieldset>
  ...
  <input type="submit" name="corrigir" value="Corrigir">
</form>

Note that with Javascript it is already possible to recover all the answers, but the easiest to implement is to fix them in PHP. The way it is, PHP will receive two arrays via POST: $_POST['id'] and $_POST['alternativa']. The array id stores the id of the issues in the database and will serve to effectively correct the issues. The array alternativa stores the answers given.

array id:

Array
(
    [0] => 5
    [1] => 32
)

Values 5 and 32 are hypothetical and represent id of the questions that were randomly selected from the bank.

array alternativa:

Array
(
    [0] => a
    [1] => c
    [2] => b
    [3] => a
    [4] => a
    [5] => d
    [6] => b
    [7] => c
    [8] => e
    [9] => c
)

The values are also hypothetical and represent the answers given to the questions.

Now just select from the database the questions whose id belongs to the vector id, something like:

"SELECT * FROM questoes WHERE id IN {$id}"

And iterate over the result, checking whether the response present in the vector alternativa is equal to the value in the column resposta.

  • where I put : $(() => { $("input[type='Submit']"). on('click', () => { console.log("Question 1: " + $('input[name="alternative[0]"]:checked'). val()); console.log("Question 2: " + $('input[name="alternative[1]"]:checked'). val(); }); });

  • See note (highlighted with yellow background). This Javascript code is not required for the solution, only exists to leave the example here verifiable.

  • I understand, but I’m still not able to fix... I’m using this command: "select * from questoes WHERE id_questao=$id and reply=$reply". But it does not work. I would have how to better clarify how to correct them?

  • you could show me how would be the correction of them?

  • I’ve been able to do as you explained, thank you very much!

Browser other questions tagged

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