Código:
la variable $indice solo podria obtner el valor correspondiente cuando se seleccione el producto del combo y se emvie su indice de seleccion es la unica forma que se me ocurre para que funcione pero al hacer eso la pagina se actualiza alterando el combo... <?php $bd=mysql_connect("localhost","root","43781406")or die("error de conexion"); mysql_select_db("tarea",$bd)or die("Error de seleccion"); $rs=mysql_query("select producto,precio from producto"); $i=0; while($row=mysql_fetch_array($rs)){ $precio[$i]=$row[1]; $producto[$i]=$row[0]; $i++; } echo '<script type="text/javascript"> function total(){ var txtimporte=document.getElementById("txttotal"); var txtcantidad=document.getElementById("txtmonton"); var txtprecio=document.getElementById("txtpre"); txtimporte.value=txtcantidad.value * txtprecio.value; } function addprecio(){ var cboproducto=document.getElementById("cbopro"); var txtprecio=document.getElementById("txtpre");'; echo'cboproducto.selectedIndex'; echo 'txtprecio.value='.$precio[$indice].'; total(); } </script>'; ?> <body> <form id="frm1" name="form1" method="post" action="sesion.php"> <table width="200" border="1" align="center"> <tr> <td>Producto</td> <td> <select name="cboproducto" id="cbopro" onchange="addprecio()"> <?php for($i=0;$i<count($producto);$i++){ echo '<option>'.$producto[$i].'</option>'; } ?> </select> </td> </tr> <tr> <td>Precio</td> <td><input type="text" name="txtprecio" id="txtpre" value="0"/></td> </tr> <tr> <td>Cantidad</td> <td><input type="text" name="txtcantidad" id="txtmonton" value="0" onchange="total()"/></td> </tr> <tr> <td>Importe</td> <td><input type="text" name="txtimporte" id="txttotal" value="0"/></td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" name="btnadd" value="Agregar al carrito" /> <a href="sesion.php?ver=si"><input type="button" name="btnver" value="Ver al carrito" /></a> <input type="hidden" name="oculto" /> </td> </tr> </table> </form> </body> </html>