Automatic result with PHP and Ajax

0

I'm doing a data entry in which I have two points that I do not know how to do in ajax or jquery.

<?php
$valor = 320; //valor en pesos
?>

Ingreso <input type="date" name="ingreso" autocomplete="off"><br>
Retiro <input type="date" name="retiro" autocomplete="off"><br>
<input type="text" name="totaldias" placeholder="Total de dias" disabled="disabled"><br>
$ <input type="text" name="valor" placeholder="Valor" disabled="disabled"><br>

What I want to do is that when the input enters the dates, I automatically place the number of days and once you have the number of days multiply them by the value.

I hope you help me. Thanks

    
asked by 19.07.2018 в 21:18
source

1 answer

0

I think this can help you. I added some things to the html to take the value of the input. You take the values of the dates, but only after filling the withdrawal input does the calculation, through an onChange event

<?php
$valor = 320; //valor en pesos
$tasa_cambio_dolares = 0.036;//1 peso argentino = 0.036
?>

Ingreso <input type="date" id="ingreso" name="ingreso" autocomplete="off"><br>
Retiro <input type="date" id="retiro" name="retiro" autocomplete="off" onChange="data(<?php echo $valor?>, <?php echo $tasa_cambio_dolares?>)"><br>
<input type="text" name="totaldias" id="totaldias" placeholder="Total de dias" disabled="disabled"><br>
$ <input type="text" name="valor" id="valor" placeholder="Valor" disabled="disabled"><br>
$ <input type="text" name="dolares" id="dolares" placeholder="Valor Dolares" disabled="disabled"><br>


<script>
    function data(valor, tasa_cambio){
        let ingreso = document.getElementById("ingreso").value;
        let retiro = document.getElementById("retiro").value;

        let fechaInicio = new Date(ingreso).getTime();
        let fechaFin    = new Date(retiro).getTime();

        let diff = fechaFin - fechaInicio;//Diferencia en milisegundos
        let dias = diff/(1000*60*60*24);//Diferencia en dias

        document.getElementById("totaldias").value = dias;
        document.getElementById("valor").value = dias*valor;
        document.getElementById("dolares").value = dias*valor*tasa_cambio;

    }

</script>

I hope you serve

    
answered by 19.07.2018 / 21:37
source