Save checkbox selected and not selected

1

I have a question, I would like to know if it is possible to save the checkboxes that I have in one detail (so many of those selected and not selected), for example, I have an attendance detail, where it shows me all the people that are registered, and I keep that in my database, but my question goes further, because I only save the ones I have selected and I can not save what I did not select, the selected ones I entered a 1 and those who are not with a 2.

My base saves the following fields:

  • id : what is the id of my assistance.
  • alumno_id : what is the student's id
  • asitencia : 1: for whom is this, 2 for whom I did not select

I have the input in detail:

<input type="checkbox" name="asistencia[]" id="asistencia[]" value="{{$al->id}}">

and my code that stores the data

if($data=$request->asistencia){
        foreach ($data as $asis=>$valor){
            $asis= New AsistenciaAlumnos();
            $asis->alumno_id = $valor;
            if(isset($request->asistencia)) {
                $asis->asistencia = 1;
            }elseif(isset($request->asistencia)==NULL){
                $asis->asistencia = 2;
            }
            $asis->save();
        }
    }

So far I can save the ones I have selected, but how can I save the ones I did not select?

Thanks

    
asked by Juanjo Medina 01.08.2018 в 17:37
source

2 answers

1

Unselected checkboxes are not sent to the backend.

For example, if you select only one or two elements, pressing in ver will list only the selected values:

jQuery(document).ready(function() {
  
  jQuery('#ver').on('click',function() {
    console.log(jQuery('#formulario').serializeArray());
  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="formulario">
<input type="checkbox" id="pedro" name="pedro" value="1"><label for="pedro">pedro</label>
<input type="checkbox" id="juan" name="juan" value="1"><label for="juan">juan</label>
<input type="checkbox" id="diego" name="diego" value="1"><label for="diego">diego</label>
<input type="button" id="ver" value="ver valores">
</form>

The way to address this behavior is to prefix a input of type hidden with the same name as the checkbox. If the checkbox is not checked, what the backend receives is the value of the hidden input. If checked, the value of the checkbox changes to the input hidden since they have the same name.

jQuery(document).ready(function() {
  
  jQuery('#ver').on('click',function() {
    console.log(jQuery('#formulario').serializeArray());
  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="formulario">
<input type="hidden"  name="pedro" value="2">
<input type="checkbox" id="pedro" name="pedro" value="1"><label for="pedro">pedro</label>
<input type="hidden"  name="juan" value="2">
<input type="checkbox" id="juan" name="juan" value="1"><label for="juan">juan</label>
<input type="hidden"  name="diego" value="2">
<input type="checkbox" id="diego" name="diego" value="1"><label for="diego">diego</label>
<input type="button" id="ver" value="ver valores">
</form>
    
answered by 01.08.2018 в 18:28
1

Maybe this can help you, in one variable you could store each value of the checbox and in another the value and status of the checkbox (active or not active)

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function (){
    $("#btnGuardar").click(function() {
            var arrChk = new ArrayAsistencia();
            var arrGeneral = $('input[name="asistencia[]"]').map(function() {
            arrChk.push(this.value, this.checked);
            return this.value;
        }).get();
        console.log("Valor: " + arrGeneral);
        console.log("Valor y Estado: " + arrChk);
});

And your input

<input type="checkbox" name="asistencia[]" id="asistencia[]" value="{{$al->id}}">
    
answered by 01.08.2018 в 21:55