How to save 0 and 1 with a checkbox?

0

I have a field called state and it is with a boolean attribute, I want to save it in the database when the input is in the checked state that stores the value 1 and the other one that stores 0, do you have any doubt about how to do it, Do you have to do with ajax, or is there another way to do it? I'm working with laravel 5.2.

Thank you very much.

<div class="switch">
<label>INACTIVO<input type="checkbox" checked><span class="lever"></span>ACTIVO</label>
</div>
    
asked by Devin Stiven Zapata Areiza 05.01.2018 в 15:03
source

1 answer

4

I think you'll need to use JavaScript and send% information%, since the default value of AJAX is not checked so you would have to convert its value to number of the following way:

$('input[type="checkbox"]').change(function(){
  $(this).val(Number(this.checked));
  
  console.log($(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="switch">
<label>INACTIVO<input type="checkbox" checked><span class="lever"></span>ACTIVO</label>
</div>
    
answered by 05.01.2018 / 15:11
source