How to take data from an input, do a calculation and play on another input?

1

I'd like to know how to get a value of input , calculate it, and play the result on another input . Example: imc = peso x (altura)² .E When the person places the height and weight in the inputs , the calculation will be done and will appear in input of the IMC.

I also have to perform calculations in other fields, but I would like help because I do not know how to do these calculations, whether in PHP itself or JavaScript?

Follow the code below:

<div>
    <input class="campo-form-c" type="text" name="pesoatual" placeholder="Peso Atual">
</div>

<div>
    <input class="campo-form-c" type="text" name="altura" placeholder="Altura">
</div>

<div>
    <input class="campo-form-c" type="number" name="idade" placeholder="Idade"></br>
</div>

<div>
    <input class="campo-form-c" type="date" name="datan" placeholder="Data de nascimento"></br>
</div>

<div>
    <input class="campo-form-c" type="text" name="medidab" placeholder="Medida do Braço" maxlength="5">
</div>

<div>
   <input class="campo-form-c" type="text" name="medidat" placeholder="Medida Tricipital" maxlength="5">
</div>

<div>
   <input class="campo-form-c" type="text" name="medidaabd" placeholder="Medida Abdominal" maxlength="5"></br>
</div>

<div>
    <input class="campo-form-c" type="text" name="imc" placeholder="IMC" maxlength="4">
</div>

<div>
    <input class="campo-form-c" type="text" name="gorduratricpital" placeholder="Gordura Tricipital" maxlength="5">
</div>

<div>
    <input class="campo-form-c" type="text" name="circunferenciabraço" placeholder="Circunferência do Braço" maxlength="5"></br>
</div>

<div>
    <input class="campo-form-c" type="text" name="circunferenciambraço" placeholder="Circunferência Muscular do Braço" maxlength="5"></br>
</div>
    
asked by anonymous 13.11.2017 / 03:49

2 answers

1

Because they are values that will be typed, it is recommended to use JavaScript to do the calculation. However, the imc formula in your question is incorrect. The correct one is:

imc = peso / (altura)² and not imc = peso x (altura)²

  

Formula: weight (in kg) divided by the square of height (in meters 1 ).

To play the calculation value in the name="imc" field, you can use querySelector to get the values of the name="pesoatual" and name="altura" fields and update the result dynamically as values are being inserted into the fields :

var peso = document.querySelector("input[name=pesoatual]");
var altu = document.querySelector("input[name=altura]");
peso.addEventListener("keyup", calcImc, false);
altu.addEventListener("keyup", calcImc, false);

function calcImc(){
   var peso_val = parseFloat(peso.value);
   var altu_val = altu.value;
   altu_val.match(/[,.]/) ? altu_val=parseFloat(altu_val.replace(",",".")) : altu_val=parseFloat(altu.value)/100;
   imc = (peso_val / (altu_val * altu_val)).toFixed(1); // "1" significa 1 casa decimal
   if(!isNaN(imc)){
      document.querySelector("input[name=imc]").value = imc;
   }
}
<div>
    <input class="campo-form-c" type="text" name="pesoatual" placeholder="Peso Atual">
</div>

<div>
    <input class="campo-form-c" type="text" name="altura" placeholder="Altura">
</div>

<div>
    <input class="campo-form-c" type="number" name="idade" placeholder="Idade"></br>
</div>

<div>
    <input class="campo-form-c" type="date" name="datan" placeholder="Data de nascimento"></br>
</div>

<div>
    <input class="campo-form-c" type="text" name="medidab" placeholder="Medida do Braço" maxlength="5">
</div>

<div>
   <input class="campo-form-c" type="text" name="medidat" placeholder="Medida Tricipital" maxlength="5">
</div>

<div>
   <input class="campo-form-c" type="text" name="medidaabd" placeholder="Medida Abdominal" maxlength="5"></br>
</div>

<div>
    <input class="campo-form-c" type="text" name="imc" placeholder="IMC" maxlength="4">
</div>

<div>
    <input class="campo-form-c" type="text" name="gorduratricpital" placeholder="Gordura Tricipital" maxlength="5">
</div>

<div>
    <input class="campo-form-c" type="text" name="circunferenciabraço" placeholder="Circunferência do Braço" maxlength="5"></br>
</div>

<div>
    <input class="campo-form-c" type="text" name="circunferenciambraço" placeholder="Circunferência Muscular do Braço" maxlength="5"></br>
</div>

1 Although the official height measurement > of the calculation of the imc is meters , for calculation purposes, the above code also accepts values in centimeters , the result will be the same.     

13.11.2017 / 05:07
0

You can do it with Javascript yourself.

Here's a simple example link

I hope it helps.

    
13.11.2017 / 04:03