necesito pasar una variable a una funcion javascript mientras ejecuto un while en php, para mostrar una tabla, basicamente lo que necesito es que la funcion javascript multiplique dos valores y arroje el resultado en un campo, pero que lo haga con cada uno de los registros, por ello intento pasar la variable $i pero no me la toma. aqui el codigo:
Código PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">
function show_alert()
{
alert("los registros fueron actualizados exitosamente");
}
function validarSiNumero(numero){
if (!/^([0-9])*$/.test(numero))
alert("El valor " + numero + " no es un número");
}
</script>
<script type="text/javascript">
function multiplicar(){
document.getElementById("base" + i).value =document.getElementById("cantidad" + i).value*document.getElementById("precio" + i).value;
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<link rel="stylesheet" href="table.css">
<body>
<?php
include "auth.inc.php";
include "conex_bd.php";
include "online.php";
function cambiarFormatoFecha($fecha){
list($anio,$mes,$dia)=explode("-",$fecha);
return $dia."-".$mes."-".$anio;
}
$numero = $_GET['recordID'];
//echo "Has pasado: $numero";
$result=mysql_query("SELECT * FROM pedidos1 where numero='$numero'",$conexion);
if(!$result){
die("el query fallo" . mysqlerror());}
//usando data
while ($row = mysql_fetch_array($result)){
?>Pto Ctrl: <?php echo $row['PTOCTRL'];?> <?php
?>Documento: <?php echo $row['DOCUMENTO'];?><br/><?php
?>Fecha: <?php echo cambiarFormatoFecha($row['FECHAEMISION']);?><br/><?php
?>Vendedor: <?php echo $row['IDPRV1VENDEDOR'];?><br/><?php
?>Observacion: <?php echo $row['OBSERVACION'];?><br/><?php
?>Cliente: <?php echo $row['IDCLI1'];?> <?php
?>Atencion: <?php echo $row['ATENCION'];?><br/><?php
?>Referencia:<?php echo $row['SUREFERENCIA'];?><br/><?php
?><strong>Base:</strong><?php echo $row['MONTOBASE'];?><br/><?php
?><strong>IVA:</strong><?php echo $row['MONTOIMPUESTO'];?><br/><?php
?><strong>Total:</strong><?php //echo $row['SUREFERENCIA'];?><?php
}
$result1=mysql_query("SELECT * from pedidos2 INNER JOIN articul1 ON pedidos2.idart1=articul1.codigo
INNER JOIN articul8 ON articul1.codigo=articul8.idart1
where pedidos2.numero='$numero' order by ITEM",$conexion);
if(!$result1){
die("el query fallo" . mysqlerror($result1));}
?>
<form name="cantidad" method="post" action="update.php?recordID=<?php echo $numero;?>">
<?php
//hago este search con la funcion COUNT para saber exactamente el numero de registros
$result2=mysql_query("SELECT COUNT(item) FROM pedidos2 where numero='$numero'",$conexion);
while ($row2 = mysql_fetch_array($result2)){
$numtotal=$row2['COUNT(item)'];
}
?>
<?php
$numero1=$_GET['recordID'];
$i=0;
while ($row1 = mysql_fetch_array($result1)){
?>
<table border="1" align="center">
<tr>
<td>Item</td>
<td>Codigo</td>
<td>Cantidad</td>
<td>Precio</td>
<td>Base</td>
<td>IVA</td>
<td>Sub-Total</td>
<td>Descripcion</td>
</tr>
<?php do {
$i++;
$cantidad=$row1['CANTIDAD'];
$montobase=$row1['MONTOBASE'];
$montoimpuesto=$row1['MONTOIMPUESTO'];
$montoprecio=$row1['PRECIO'];
$subtotal=$row1['SUBTOTAL'];
$nombrecant="cantidad[$i]";
$nombreprecio="precio[$i]";
$nombremontobase="montobase[$i]";
$nombremontoimpuesto="montoimpuesto[$i]";
$nombresubtotal="subtotal[$i]";
$id="id[$i]";
?>
<input type="hidden" name="<?php echo $id;?>" value="<?php $cantidad['id'];?>" />
<tr>
<td><?php echo $row1['ITEM']; ?></td>
<td><?php echo $row1['IDART1']; ?></td>
<td><input type="text" value="<?php echo (int) $cantidad; ?>"name="<?php echo $nombrecant ?>" size="2"id="cantidad<?php echo $i;?>" onChange="validarSiNumero(this.value);" onKeypress="multiplicar(<?php echo $i;?>);"/></td>
<td><input type="text" value="<?php echo $montoprecio?>" name="<?php echo $nombreprecio?>" size="4"id="precio<?php echo $i;?>" readonly="readonly" onKeypress=""/></td>
<td><input type="text" value="<?php echo $montobase; ?>"name="<?php echo $nombremontobase ?>" size="4"id="base" readonly="readonly" /></td>
<td><input type="text" value="<?php echo $montoimpuesto; ?>"name="<?php echo $nombremontoimpuesto ?>" size="2"id="impuesto" onChange="validarSiNumero(this.value);" onChange=""/></td>
<td><input type="text" value="<?php echo $subtotal; ?>"name="<?php echo $nombresubtotal ?>" size="3"id="subtotal" onChange="validarSiNumero(this.value);" onChange=""/></td>
<td><?php echo $row1['DESCRIPCION']; ?></td>
<td><a href="borra.php?recordID=<?php echo $numero1 ?>&id=<?php echo $row1['ITEM'] ?>&numtotal=<?php echo $numtotal ?>"><img src="images/borrar.png" width="15" height="15" /></td>
</tr>
<?php } while ($row1 = mysql_fetch_assoc($result1));
}
?>
</table>
<br />
<?php
print "<input type='submit' value='actualizar' onClick='show_alert()' onSubmit='validarSiNumero(this.value) onSubmit='';' />";
print "</form>";
//cerrar conexion
mysql_close($conexion);
?>
</body>
</html>