Require at least 1 checkbox

3

I have a table that in each line has two options checkbox, yes and no, as I show in the image:

Codetomountthetable:

$y=0;while($rows_cursos=mysqli_fetch_array($resultado_cursos)){$tabela1.='<tr>';$tabela1.='<td><inputtype="text" readonly="true" size="20" name= "Produto['.$y.']" id= "Produto" value="'.$rows_cursos['Descricao'].'"></td>';

$tabela1 .= '<td style="float:center"> <input type="checkbox" name= "Sim['.$y.']">';

$tabela1 .= '<td style="float:center"> <input type="checkbox" name= "Nao['.$y.']">';

$tabela1 .= '<td> <textarea type="text" id="Observacao" name="Observacao['.$y.']" rows="2" cols="30"></textarea>';

$tabela1 .= '</tr>'; 
$y++;
}

I wanted to force you to fill in one of the checkboxes on each of the lines and if any of the lines were not filled one of the checkbox would display an alert to warn you that it is missing a line.

    
asked by anonymous 29.11.2018 / 17:31

2 answers

9

You can change the element type of checkbox to radio and include the attribute required to make it mandatory:

<form>
  <div>
    <label for='sabonete-liquido'>Sabonete Líquido</label>
    <input type='radio' name='sabonete' value='y' required id='sabonete-liquido'>
    <input type='radio' name='sabonete' value='n'>
  </div>
  <div>
    <label for='locao-corporal'>Loção Corporal</label>
    <input type='radio' name='locao-corporal' value='y' required id='locao-corporal'>
    <input type='radio' name='locao-corporal' value='n'>
  </div>
  <div>
    <label for='po-de-talco'>Pó de Talco</label>
    <input type='radio' name='po-de-talco' value='y' required id='po-de-talco'>
    <input type='radio' name='po-de-talco' value='n'>
  </div>

  <!-- ... -->

  <button type='submit'>Enviar</button>
</form>
    
29.11.2018 / 17:42
4

@Renan's answer easily solves what you want, but I'd like to add citing problems in your code:

  

First of all you should do what is suggested: change checkbox   by radio . You should use checkbox only when it is multiple   choice. When you can only choose 1 option, you should use radio.

1. Repeating id's

When looping while will repeat id= "Produto" on each line, This is incorrect because an id must be unique. Change by class: class= "Produto" . You are also repeating another id: id="Observacao" . Swap with class="Observacao" .

2. Names and values of radios

radio options must have the same name . You should use the same name in each radiobutton pair of each line and assign each of the pair a value that will differentiate the option.

For example, the radius of "yes" can be given the value of s and that of "no" the value of n , and name can be produto :

<input type="radio" name= "produto['.$y.']" value="s" required>
<input type="radio" name= "produto['.$y.']" value="n">

Finally, you need to close the <td> of the table, such as this line:

                                                                                   ↓↓↓
$tabela1 .= '<td style="float:center"> <input type="checkbox" name= "Sim['.$y.']"></td>';
    
29.11.2018 / 17:54