Form insert with checkbox

Asked

Viewed 38 times

1

I have this form, where I have a checkbox:

<div class="table-responsive">  
<?php
$user = (!empty($_GET['codigo'])) ? $_GET['codigo'] : '';
?>
<input type="hidden" id="codigo" name="codigo" value="<?php echo htmlentities( $user ) ; ?>" /> 
<div class="form-group">
<label for="IniciarTarefa" id="acao"></label>
<button align="center" type="button" class="btn btn-warning" onclick="myFunction()">Iniciar Tarefa</button>
<input type="hidden" id="IniciarTarefa" name="IniciarTarefa"/>
<input type="hidden" name="Colaborador" id="Colaborador" value="<?php echo $_SESSION['usuarioNome']; ?>">
</div> 

<div class="table-responsive">  
<label for="IniciarTarefa">Tarefa a Par</label>
<input type="checkbox" name="check" id="check" value="Sim"/> 
<div class="form-group input-group input-group-lg">
    <span class="input-group-addon">
    <span class="glyphicon glyphicon-user"></span>
    </span>
    <select class="form-control" name="Acompnhante" id="Acompnhante" style="display:none" required="" placeholder="Acesso">
      <option></option>
      <?php        
         $sql = "SELECT * FROM raddb.usuarios ORDER BY nome ASC";
         $qr = mysqli_query($conn, $sql);
         while($ln = mysqli_fetch_assoc($qr)){
            echo '<option value="'.$ln['id'].'">'.$ln['nome'].'</option>';
         }
      ?>        
    </select>
  </div>
</div> 

<div class="div" style="float: left">
<div class="form-group">
<input type="hidden" id="tarefa" name="tarefa" value="Lateral Direito">
<button type="button" class="btn btn-info btn-sm botao" onclick="inserir_registo();if(confirm('Pretende registar esta atividade?')) this.disabled=true;">Lateral Direito</button><button type="button" name="abrir"><span class="glyphicon glyphicon-comment"></span></button>
<div class="form-group" id="comentario" style="display:none">
<textarea id="Observacao" name="Observacao" style="color: black;"></textarea>
</div>
</div>
</div>

and do the insert in this way:

function inserir_registo()
{    
    var dadosajax = {
        'codigo' : $("#codigo").val(),
        'IniciarTarefa' : $("#IniciarTarefa").val(),
        'Colaborador' : $("#Colaborador").val(),
        'tarefa' : $("#tarefa").val(),
        'check' : $("#check").val(),
        'Acompnhante' : $("#Acompnhante").val(),
        'Observacao' : $("#Observacao").val()
    };
    $.ajax({
        url: './insertarefa',
        type: 'POST',
        cache: false,
        data: dadosajax,
        error: function(){

        },
        success: function(result)
        { 

        }
    });
}

But when I do insert, even if I don’t select the checkbox, the value is always entered, but you should only enter it when you select it.

  • 1

    Puts everything into function within a if($("#check").is(":checked")){ // ajax aqui }. So Ajax will only be called if he has checked.

  • @Sam but I want the other fields to be entered in the case of checkbox is that I want you to enter if I’m checked.

1 answer

1


Use a ternary operator.

If checked, the value of check goes with the value of checkbox, if not checked, the check will be empty:

'check' : $("#check").is(":checked") ? $("#check").val() : '',

Browser other questions tagged

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