How to return checked check value via ajax

1

I have a student enrollment form. It's all ready, the only thing I'm having difficulties is to return the value of a checkbox, which in the register refers to whether the student is active or not.

In the form it looks like this:

<label>Ativo</label>
<input type="checkbox" id="ativo" name="disponivel" value="ativo">

In ajax I get the value of it

$.ajax({
       method: 'POST',
       url: 'ajax/cadastraAluno.php',
       data: {
              // outros dados
              ativo: $("#ativo").is(':checked')
             },

In the file registerAluno.php I use the data form submitted to make the student record in the table.

cadastraAluno.php

$ativo = $_POST['ativo'];

if($ativo == true){
    $codativo = '1';
} else {
    $codativo = '0';
}

I can usually register all other data except this checkbox.

In the database, the column that records this data is as "tinyint (1)"

For me it is indifferent how it will be on the bench, it can be int, boolean, anything, as long as I can differentiate with the data, whether the student is active or not.

    
asked by anonymous 05.12.2015 / 13:50

1 answer

0
Tente assim

<label>Ativo</label>  
<input type="checkbox" id="ativo" name="disponivel" value="1">    


$.ajax({  
   method: 'POST',  
   url: 'ajax/cadastraAluno.php',  
   data: {  
          // outros dados  
          ativo: $("#ativo").val();  
         }    

**cadastraAluno.php**  
$codativo = $_POST['ativo'];  

if($codativo!=1){  
$codativo = '0';  
}    
    
05.12.2015 / 18:41