lundi 1 avril 2019

Add a variable to input beside a checkbox (onchange event) where there the table is shown from a "while"

The user has to choose a date first, I get the value from this input. The part below that is a list from mysql that looks like this: Name / Checkbox / date input" where upon clicking/changing the checkbox for any of the Names, the "date input" will change into the same date from the first part so that it can be later saved into the db.

here's what it looks like on .php

<label for='superv' >Nombre de Equipo</label>
            <select name='superv' id='superv'>
            '<option></option>'";
                while($row = mysqli_fetch_array($superv))
                {
                    echo "<option value='$row[IDSUPERVISOR]'>$row[NOMEQUIPO]</option>";
                }           
        echo"    </select>                     
        <label for='cantp' >Cantidad de Personas</label>
            <input type='text' id='cantp' name='cantp' readonly>
        <label for='razon' >Fecha Inicio</label>
            <input type='date' id='fechai' name='fechai' class='fechai' >
        <br>";

        while($row = mysqli_fetch_array($result)){
         echo ' <tr>
                <td>'.$row['NOMBRE'].' '.$row['APELLIDO_P'].'</td>
                <td class="text-center"><input class="boton" type="checkbox" name="checkbox[]" value="'.$row['IDLIBRETA_PERSONAL'].'"></td>
                <td><input type="date" id="fechai-cambio" name="fechai5" ></td>
                <td>'.$row['NOMBRE'].'</td>

                </tr>';
        }    
        echo "<button id='btn_form' type='button' onclick='realizaGrupo()'>Grabar</button>";

and here's what I have so far on the javascript side of things:

$('.boton').change(function () {
    var fechai1 = $('#fechai').val();
    var fechaf1 = $('#fechaf').val();
    var horai = $('#horai').val();
    var horaf = $('#horaf').val();
    $('#fechai-cambio').val(fechai1);
    $('#fechaf-cambio').val(fechaf1);
    $('#horai-cambio').val(horai);
    $('#horaf-cambio').val(horaf);

    var fechai = new Date(fechai1);
    var month = fechai.getUTCMonth() + 1; 
    var day = fechai.getUTCDate();
    var year = fechai.getUTCFullYear();
    var newdate = year + "-" + month + "-" + day;

    var fechaf = new Date(fechaf1);
    var month1 = fechaf.getUTCMonth() + 1; 
    var day1 = fechaf.getUTCDate();
    var year1 = fechaf.getUTCFullYear();
    var newdate1 = year1 + "-" + month1 + "-" + day1;

    var fechaI = new Date(newdate)
    var fechaF = new Date(newdate1)

var difM = fechaF - fechaI // diferencia en milisegundos
var difD = difM / (1000 * 60 * 60 * 24) // diferencia en dias

console.log(difD)

$('#dias').val(difD);


});




Aucun commentaire:

Enregistrer un commentaire