How to return check value marked via ajax

Asked

Viewed 67 times

1

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

On the form he’s like this:

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

In ajax I take the value of it

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

In the registration fileAluno.php I use the submitted form data to record the student in the table.

cadastraAluno.php

$ativo = $_POST['ativo'];

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

I can register all other data normally, except this checkbox.

In the bank, the column recording this data is "tinyint(1)"

To 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, if the student is active or not.

  • 1

    If in the comic book you have tinyint then you must pass a number and not a string. Try to take this if/Else and put it like this: $codativo = $ativo ? 1 : 0;. Worked?

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';  
}    
  • is it possible to pick up val() from a checkbox? for me it was only possible to take the check from the checkbox with . attr(), . prop() or . is()

Browser other questions tagged

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