Check multiple Radio type input in a simulated

Asked

Viewed 212 times

0

Well, I’m creating a website and I need to validate the radio input fields. I thought about using JS, but it didn’t work. As stated in the topic, there are several fields that I need to validate. follows the code I have to be validated:

    <h1 class="topico-titulo" ><?php echo"$nome";?></h1>                
                                <?php 
    $sql="select * from questoes where id_materia=$materia order by rand() limit 10";
                                    $resultado=mysqli_query($conexao,$sql);?>
                                    <div style="margin-left: -28px;margin-right: 10px; margin-top: 10px;" >


 <form name="simulado" action="correcao.php" method="post">
 <ul style="list-style-type:none;">
    <?php $i = 0; ?>

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

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

<li> <fieldset  >
  <legend >Questão <?= $i+1; ?></legend>
 <legend > <i>cod.<?= $id; ?></i> </legend>
  <p > <?= $pergunta; ?></p>
  <?php if(!empty($imagem)) { $arquivo = substr($imagem, -3); if($arquivo=='jpg'){?>
  <center>
  <p > <img src="Imagens/<?php echo $imagem; ?>" width="80%" heigth="70%"/>     </p>
  <?php }}?></center>
  <?php $_SESSION['pergunta'.$i]=$pergunta; ?>
  <?php $_SESSION['resolucao'.$i]=$resolucao; ?>
         <input type="hidden" name="questao[<?= $i; ?>]"    value"<?php echo $pergunta; ?>">
         <input type="hidden" name="id[<?= $i; ?>]"          value="<?php echo $id; ?>">
         <input type="hidden" name="resposta[<?= $i; ?>]"    value="<?php echo $resposta; ?>">
         <ul style="list-style-type:none;margin-left:-6%;">
  <li> <label><input type="radio"  name="alternativa[<?= $i; ?>]" value="A"> <?= $a; ?></label><br/> </li>
  <li> <label><input type="radio"  name="alternativa[<?= $i; ?>]" value="B"> <?= $b; ?></label><br/></li>
  <li> <label><input type="radio"  name="alternativa[<?= $i; ?>]" value="C"> <?= $c; ?></label><br/></li>
  <li> <label><input type="radio"  name="alternativa[<?= $i; ?>]" value="D"> <?= $d; ?></label><br/></li>
  <li> <label><input type="radio"  name="alternativa[<?= $i; ?>]" value="E"> <?= $e; ?></label></li>
  </ul>
 </fieldset></li> 

 <?php $i++; ?>

  <?php endwhile; ?>

  <input type="submit" name="corrigir" value="Corrigir Simulado">

   </form>
   </div>
   </div>

Any attempt to help will be valid and recognized, if it has not been very clear my objective, please comment and I will illustrate and clarify any doubts.

From now on, very grateful!

1 answer

0


  • I didn’t understand very well, how would I know if one of the group of 5 buttons was selected, among 10 other groups? could exemplify me with a code?

  • 1

    @Dwcleb when you create the radio group, you will name all with the same attribute name="" but with the attribute value="" if only one of these input has the attribute required="" of HTML5 then it will become mandatory. for example: http://jsfiddle.net/2du3s/1/

  • 1

    Actually, his mistake is not putting name the same in radios (which I also always commit). Other: it is good to indicate the degree of compatibility of the required attribute: http://caniuse.com/#feat=form-validation

  • exactly @Daniel

  • @Rafaelrn, the name had to be the way it is, I managed to solve the problem already... On the correction page I was calling their names... every question, which in total are 10, their name changes the numbering. Gave p I fix using a loop and checking the database, Thanks guys!

Browser other questions tagged

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