How to save the checkbox value by choosing only one of these?

Asked

Viewed 137 times

0

I have a form with three checkbox, I want to save the selected value of some of these, but in one of them when clicked, I bring two more checkbox and a input (text) to add additional information to them.

In the code when I test, only the third checkbox is saving in the database, when I click on the first or second, nothing happens.

<form>
    <div class="container">
        <label>                
            <input id="lbm-chk-grau1" value="1 Grau" name="escolaridade[]" type="checkbox"> 1&#186; grau
        </label>

        <label>
            <input id="lbm-chk-grau2" value="2 Grau" name="escolaridade[]" type="checkbox"> 2&#186; grau
        </label>

        <label>
           <input id="lbm-chk-grau3" value="3 Grau" name="escolaridade[]" type="checkbox"> 3&#186; grau
        </label>

        <div class="form-checkbox">
             <label>               
                <input id="lbm-chk-compl" value="Completo" name="superior[]" type="checkbox"> Completo
             </label>
             <label>
                <input id="lbm-chk-imcopl" value="Incompleto" name="superior[]" type="checkbox"> Incompleto
             </label>
                <br><br>
            <input type="text" name="IES" placeholder="Nome da Institui&ccedil;&atilde;o" class="form-control">                                              
        </div>
   </div>

 <div class="container">
      <button type="submit" class="btn btn-link" name="btn-registro">Salvar Informa&ccedil;&otilde;es</button>                              
 </div>

</form>

How I try to save with PHP in the database:

$chkAuxEscol =  is_array($_POST['escolaridade'])
                  ? implode(', ', $_POST['escolaridade'])
                  : $_POST['escolaridade'];
$chkSupAux =  is_array($_POST['superior'])
                  ? implode(', ', $_POST['superior'])
                  : $_POST['superior'];

if ( (isset($chkAuxEscol) ) { 

   $chkEscolaridade = $chkAuxEscol;

   if ( isset($chkSupAux) )  
   {                                    
       $chkSuperior = $chkSupAux;
       $IESParticipante = security_data_2($_POST['participanteIES']); 
   }
}

// Outra forma que tentei salvar
foreach($chkAuxEscol as $key => $auxEscol) {

    switch($auxEscol) {
       case "lbm-chk-grau1" :
              $chkEscolaridade = "1º Grau";
              break;
       case "lbm-chk-grau2" :
              $chkEscolaridade = "2º Grau";
              break; 
       case "lbm-chk-grau3" :
              $chkEscolaridade = "3º Grau";
              break;  
       default : break; 
       $i++;         
    }                         
} 

foreach ($chkSupAux as $key => $supAux)
{
    switch($supAux) {
           case "lbm-chk-compl" :
                  $chkSuperior = "Superior Completo";
                  break;
           case "lbm-chk-imcopl" :
                  $chkSuperior = "Superior Incompleto";
                  break;
            default : break;                  
    }      
}

$pdo = Database::connect();

$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$sql  =  "INSERT INTO participantes(escolaridade, escolaridade_detalhes, escolaridade_instituicao) "; 
$sql .=  "VALUES(? , ?, ?)"; 

$q = $pdo->prepare($sql);
$q->execute(array($chkEscolaridade,$chkSuperior,$IESParticipante));                     
  • If you only want the value of a checkbox, it would not be better to use a radiobox?

1 answer

2

  1. Make explicit the form configuration with action="post".
  2. Make sure the tags html name="escolaridade[]" be the same thing you want in $_POST. Example, if name="escolaridade" use $_POST['escolaridade'] or use $_POST['escolaridade[]'] as it is in your html.

If it doesn’t work.. mark all three options and paste the result of $_POST['escolaridade'].

  • It didn’t work, I already changed this block with foreach and switch and still no save.

  • @Diego.Santos is another problem that has nothing to do with getting information from the form.

Browser other questions tagged

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