calculate percentage

1

I have a problem when calculating the percentage I have the following fields: amount, percentage, profits my problem is that the calculation does it wrong, the percentage field I select it with a select and from there it will proceed to take the calculation of percentage once the amount is entered.

append the code

<script language="javascript">
 function multiplica(form){
 var resultado;
 var resultado2;
 var x=0;
 var y=0;
 x = parseInt (form.monto_trasferido.value);
 y = parseInt (form.id_porcentaje.value);

 resultado = x * y/100;
 form.ganancia.value=resultado;


  resultado2 = x + y;
  form.total.value=resultado2;

   }
  </script>

the input and select

   <div class="input-field col s12 m3">
   <input id="icon_prefix"  type="text" class="black-text" name="monto_trasferido" autocomplete="off" title="Disculpa, seleccione una fecha" pattern="[0-9]{5,7}" required/>
   <label for="cedula" class="black-text ">Monto Transferencia:</label>
   </div>



   <div class="input-field col s12 m3">
   <select name="id_porcentaje" id="id_porcentaje" class="browser-default" onblur="multiplica(this.form)" required/>
   <option value="" disabled selected>Porcentaje:</option>
   <?php
    $consulta = $DB_con->query("SELECT * FROM porcentaje ORDER BY id_porcentaje");
    while ($linea = $consulta->fetch(PDO::FETCH_ASSOC)) {
    ?>
    <option value="<?php echo $linea['id_porcentaje'] ;?>"><?php echo 
    $linea['porcentaje'] ;?>%</option>
    <?php
       }
     ?>
   </select>
   </div>


   <div class="input-field col s12 m3">
   <input id="icon_prefix"  type="text" class="black-text" name="ganancia"  readonly/>
   <label for="cedula" class="black-text ">Ganancia:</label>
   </div>


   <div class="input-field col s12 m3">
   <input id="icon_prefix"  type="text" class="black-text" name="total" autocomplete="off" required/>
   <label for="cedula" class="black-text ">Total:</label>
   </div>

a sample image

    
asked by yoclens 12.07.2017 в 06:42
source

2 answers

1

The problem is that you are adding the monto that the user enters plus the número del porcentaje , and what you need is to add the monto plus the calculation result of monto * porcentaje / 100 .

/*
* Devuelve la suma del monto más el porcentaje
* Monto = 1000
* Porcentaje = 50%
* Retorna 1050
*/
resultado2 = x + y;

/*
* Devuelve la suma del monto más el resultado de calcular el porcentaje sobre el monto
* Monto = 1000
* Porcentaje = 50%
* Retorna 1500
*/
resultado2 = x + resultado;
form.total.value = resultado2;
    
answered by 12.07.2017 / 07:02
source
0

The problem is that you are doing wrong the sum, you are supposed to add the amount plus the result of calculating amount * percentage / 100

    
answered by 11.08.2017 в 05:58