Validate multiple forms at once - Simulated

1

I'm creating a mock site and I'm having a hard time fixing it because I do not know how to validate numerous forms at once.

What happens:

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

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

<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>
    
asked by anonymous 11.03.2017 / 00:08

1 answer

1

First, you do not need multiple forms on your page. Only one gives the message. The only change you need to make to this is to differentiate the fields between the issues. You can set the value of input as lists, set the index of the list. 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 HTML similar to:

  

CSS code was just to hand leave the result so ugly and the JavaScript is to execute something when the button is pressed.

$(() => {

  $("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><formaction="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 retrieve all the answers, but the easiest one to implement is to correct them in PHP. As it stands, PHP will receive two arrays via POST: $_POST['id'] and $_POST['alternativa'] . The array id stores the id of the questions in the database and will serve to effectively correct the issues. The array alternativa stores the given answers.

array id :

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

Values 5 and 32 are hypothetical and represent the 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 you just select from the database the questions whose id belongs to the id vector, something like:

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

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

    
11.03.2017 / 02:05