Ver Mensaje Individual
  #27 (permalink)  
Antiguo 23/12/2011, 00:29
Avatar de kimmy
kimmy
 
Fecha de Ingreso: julio-2008
Mensajes: 841
Antigüedad: 15 años, 9 meses
Puntos: 15
Respuesta: php sumar input text con variable sin hacer submit

Siguiendo en la nómina, logré calcular el total de deducciones pero no me funciona el total devengado. Este es mi código:

Código Javascript:
Ver original
  1. function calcula(dias, sueldo_diario, indice) {
  2.     // Va sin formato, eso tendrás que preguntarlo en el foro de Javascript
  3.     document.getElementById('sueldo_quinc' + indice).value = dias * sueldo_diario;
  4. }
  5.  
  6. function deducciones(otros_desc, ivss, paro_for, lph, indice2) {
  7.     document.getElementById('total_deducc' + indice2).value = parseFloat(otros_desc) + parseFloat(ivss) + parseFloat(paro_for) + parseFloat(lph);
  8. }
  9.  
  10. function total_dev(otros_ing, sueldo_quinc, total_deducc, indice3) {
  11.     document.getElementById('total_deveng' + indice3).value = (parseFloat(otros_ing) + parseFloat(sueldo_quinc)) - parseFloat(total_deducc);
  12. }

y el fomulario:

Código PHP:
Ver original
  1. <form action="nomina_reg.php" method="post" >
  2. <table width="750" border="0" class="tabla2">
  3. <tr>
  4. <td colspan="5" class="td2" bgcolor="#95bce2">UNIDAD EDUCATIVA</td>
  5. </tr>
  6. <tr>
  7. <td colspan="5">NOMINA PARA EL PAGO DE SUELDOS EMPLEADOS</td>
  8. </tr>
  9. <tr>
  10. <td width="7%">Desde:</td>
  11. <td width="15%">&nbsp;</td>
  12. <td width="7%">Hasta:</td>
  13. <td width="16%">&nbsp;</td>
  14. <td>&nbsp;</td>
  15. </tr>
  16. </table>
  17. <br />
  18. <table width="750" border="1"  bordercolor="#95bce2"  class="tabla3">
  19. <tr bgcolor="#95bce2 ">
  20.     <td width="3%" class="td3">Nº</td>
  21.     <td width="3%" class="td3">CI</td>
  22.     <td width="10%" class="td3">APELLIDOS</td>
  23.     <td width="11%" class="td3">NOMBRES</td>
  24.     <td width="4%" class="td3">DIAS</td>
  25.     <td width="8%" class="td3">SUELDO BÁSICO</td>
  26.     <td width="8%" class="td3">SUELDO DIARIO</td>    
  27.     <td width="8%" class="td3">SUELDO B. QUINCENA</td>
  28.     <td width="8%" class="td3">IVSS</td>
  29.     <td width="8%" class="td3">PARO FORSOZO</td>
  30.     <td width="8%" class="td3">LPH</td>
  31.     <td width="8%" class="td3">OTROS INGRESOS</td>
  32.     <td width="8%" class="td3">OTROS DESCUENT</td>
  33.     <td width="8%" class="td3">TOTAL DEDUCCIONES</td>
  34.     <td width="8%" class="td3">TOTAL DEVENGADO</td>
  35.   </tr>
  36.  
  37.   <?php
  38. $indice = 1;
  39. $indice2 = 1;
  40. [B]$indice3 = 1;[/B]
  41. $rs = mysql_query("SELECT * FROM empleados ORDER BY id_empleado ASC");
  42. while($result = mysql_fetch_array($rs)) {
  43.     $ivss = number_format(($result['sueldo_basico']*4/100),2,'.','');
  44.     $paro_for = number_format(($result['sueldo_basico']*0.5/100),2,'.','');
  45.     $lph = number_format(($result['sueldo_basico']*1/100),2,'.','');
  46.     $sueldo_diario = $result['sueldo_basico']/30;
  47. ?>
  48.     <tr>
  49.     <td><?php echo $result['id_empleado']; ?><input type="hidden" name="id_empleado[]" value="<?php echo $result['id_empleado']; ?>" /></td>
  50.         <td><?php echo $result['ci']; ?></td>
  51.         <td><?php echo "{$result['papellido']} {$result['sapellido']}"; ?></td>
  52.         <td><?php echo "{$result['pnombre']} {$result['snombre']}"; ?></td>
  53.         <td><input name="dias[]" type="text" id="dias<?php echo $indice; ?>" size="2" value="" onblur="calcula(this.value, '<?php echo $sueldo_diario; ?>', <?php echo $indice; ?>);" /></td>
  54.         <td><?php echo $result['sueldo_basico']; ?></td>
  55.         <td><?php echo $sueldo_diario; ?></td>
  56.         <td><input name="sueldo_quinc[]" type="text" id="sueldo_quinc<?php echo $indice; ?>" size="5" value="" readonly="readonly" /></td>
  57.         <td><?php echo $ivss; ?></td>
  58.         <td><?php echo $paro_for; ?></td>
  59.         <td><?php echo $lph; ?></td>
  60.         [B]<td><input name="otros_ing[]" type="text" id="otros_ing<?php echo $indice3; ?>" size="5" value="" onblur="total_dev(this.value, '<?php echo $sueldo_quinc; ?>', '<?php echo $total_deducc; ?>', <?php echo $indice3; ?>);" /></td>[/B]
  61.         <td><input name="otros_desc[]" type="text" id="otros_desc<?php echo $indice2; ?>" size="5" value="" onblur="deducciones(this.value, '<?php echo $ivss; ?>', '<?php echo $paro_for; ?>', '<?php echo $lph; ?>', <?php echo $indice2; ?>);" /></td>
  62.         <td><input name="total_deducc[]" type="text" id="total_deducc<?php echo $indice2; ?>" size="5" value="" readonly="readonly" /></td>
  63.        <td><input name="total_deveng[]" type="text" id="total_deveng<?php echo $indice3; ?>" size="5" value="" readonly="readonly" /></td>
  64.     </tr>
  65. <?php
  66.     $indice ++;
  67.     $indice2 ++;
  68.     [B]$indice3 ++;[/B]
  69. } // fin de while

Me imagino que no funciona porque $sueldo_quinc y total_deducc son array pero no se como ponerlos....
__________________
Caminando con el corazón partío