Show checkbox already marked with jquey according to a MySQL database record

2

I have a fullcalendar and clicking on some day will show me a form, the user enters the normal data and must select some people assigned to that event with a checkbox. ( There everything is fine ).

Calendar image with events already created

Image of the registration form

The user clicking on an already created event will show the records that he has already entered and which is already stored in the database:

Image of the form with the data that the user already registered

Image of the sample of the database, and the assigned field

Jquery code where it shows the information of the events in the inputs:

eventClick:function(calEvent,jsEvent,view){

                $('#btnAgregar').prop("hidden",true);
                $('#btnModificar').prop("hidden",false);
                $('#btnFinalizado').prop("hidden",false);


                //Mostrar la información del evento en los inputs           
                FechaHoraCorreo= calEvent.correo.split(" ");
                $('#txtFechaCorreo').val(FechaHoraCorreo[0]);
                $("#txtHoraCorreo").val(FechaHoraCorreo[1]);
                FechaHoraInicio= calEvent.inicio.split(" ");
                $('#txtFechaInicio').val(FechaHoraInicio[0]);
                $("#txtHoraInicio").val(FechaHoraInicio[1]);
                FechaHoraFin= calEvent.fin.split(" ");
                $('#txtFechaFin').val(FechaHoraFin[0]);
                $("#txtHoraFin").val(FechaHoraFin[1]);
                FechaHoraEntrega= calEvent.entrega.split(" ");
                $('#txtFechaEntrega').val(FechaHoraEntrega[0]);
                $("#txtHoraEntrega").val(FechaHoraEntrega[1]);
                $('#txtIdCumplimiento').val(calEvent.idcumplimiento);
                $('#txtTrabajo').val(calEvent.trabajo);
                $('#txtIdEjecucion').val(calEvent.idejecucion);
                $('#txtPm').val(calEvent.pm);
                $('#txtComercial').val(calEvent.comercial);
                $('#txtIdMes').val(calEvent.idmes);


                $("#ModalEventos").modal();
            }

What do I do so that when the user clicks on an event already created, I show the checkboxes already checked according to what the user checked when they registered?

If, for example, you click on Event 1, check out Erick Gaming, or if you click on event 2, show me Andres Medina and Nelson Barrios and so on with the other events that you are creating.

    
asked by Jairo Arturo Cifuentes 29.08.2018 в 14:54
source

3 answers

1

I've already solved it:

It's just doing this and you're done!

                let seleccionados = calEvent.asignados.split(',');

                jQuery(document).ready(function() {
                    seleccionados.forEach(function(nombre) {
                        jQuery('.txtAsignados[value="${nombre}"]').prop('checked',true);
                    });
                });

                jQuery('.txtAsignados').prop('checked',false);
    
answered by 19.09.2018 в 23:31
0

Try this:

var idEvento = $("#idevento").val();
$.ajax({
    data:  idEvento,
    url:   '?URL_retorna_los_asignados_del_evento',  
    type:  'post',
    dataType: "json",
    success:  function (response){ //response es un array
        $('input[type=checkbox]').each(function() {//Recorre los checks
            response.forEach(function(e){
                if(e == $(this).prop("name")){//Asumo el valor del label del check en la propiedad name del check
                    $(this).attr("checked","checked"); // Seteamos los asignados                
                }
            });          
        });
    }
})
    
answered by 29.08.2018 в 15:58
0

Just as you are using Jquery to give value to the other fields, you can use the checkedbox's checked property:

$('#idDeTuCheckbox').prop('checked', true);

If you have stored the values of the checkbox, as you have the rest of the fields, it should not be more of a problem.

EDIT

<?php

    $contador = 1;

    foreach ($sql8 as $row) {   ?>
        <input type="checkbox" id="miID-<?php $contador ?>">
        <?php $contador++;
    } ?>

With that you would get to have each of your checkboxes with a different ID. Then you should see how to determine which ones you want to mark or not, depending on how you have stored the checkboxes in your database.

    
answered by 29.08.2018 в 15:19