Ver Mensaje Individual
  #1 (permalink)  
Antiguo 04/10/2013, 17:58
ruben_chirinos_1985
Invitado
 
Mensajes: n/a
Puntos:
Problemas en mostrar combobox en filas agregadas autmaticamente

hola amigos como estan disculpen que haya abierto otro tema respecto a registro de facturas, es qtengo dias intentando de hacer y no logro hacer nada de lo que quiero, aqui consegui un ejemplo y lo adapte a mi necesidad pero tengo un problema de como mostrar el combobox llenado de la bd en las filas que voy agregando dinamicamente con javascript

Código PHP:
Ver original
  1. <?php
  2. if(isset($_POST['ok'])){
  3.  
  4. for ($i=1;$i<=$_POST["var_cont"];$i++)
  5.  {
  6. echo "Numero de Fila: " ; echo $i;
  7. echo "Codigo: ";  echo $_POST["code_$i"];
  8. echo "Nombre: "; echo $_POST["name_$i"];
  9. echo "Precio: "; echo $_POST["precio_$i"];
  10. echo "Cantidad: "; echo $_POST["cant_$i"];echo "<br>";
  11.  
  12.  }
  13.  
  14. }
  15. ?>
  16.  
  17. <html>
  18. <head>
  19. <title>PRUEBA AGREGAR FILAS
  20. </title>
  21. </head>
  22.  
  23. <body>
  24. <form id="form" name="form" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" onSubmit="asigna()">
  25. <br>
  26.  
  27. <table border="1" id="tabla" bordercolor="#FFCC33" cellspacing="1">
  28.  
  29. <tr align="center">
  30.   <td colspan="4"><font color="blue" size="1">DETALLE DE FACTURAS </font></td>
  31.   </tr>
  32. <tr align="center">
  33.   <td><font color="blue" size="1">COD</font></td>
  34. <td><font color="blue" size="1">NOMBRE</font></td>
  35. <td><font color="blue" size="1">PRECIO</font></td>
  36. <td><font color="blue" size="1">CANTIDAD</font></td>
  37. </tr>
  38.  
  39. <tr>
  40.   <td><input type="text" size="4" name="code_1" /></td>
  41. <td><select name="name_1" id="name_1" maxlength="10" onChange="document.getElementById('precio_1').value=this.options[this.selectedIndex].getAttribute('name_1'); actualizar_importe()" required="required"><option value="">-- ELIJE PRODUCTO --</option>
  42.                     <?php
  43. $qry=mysql_query("select * from productos");
  44. $row=array();
  45. while($r=mysql_fetch_assoc($qry)){
  46. $row[]=array($r['cod_producto'],$r['producto'],$r['precio']);
  47. }
  48.                       foreach($row as $v){ ?>
  49.                     <option  name_1="<?php echo $v[2] ?>" value="<?php echo $v[0] ?>"><?php echo $v[1] ?></option>
  50.                     <?php }?>
  51.       </select></td>
  52. <td><input type="text" size="8" name="precio_1" id="precio_1"/></td>
  53. <td><input type="text" size="8" name="cant_1"/></td>
  54. <input type="hidden" name="var_cont">
  55. </tr>
  56. </table>
  57.  
  58.  
  59. <input type="button" name="b1" value="Agrega Producto" onClick="addRowX()">
  60. <input type="button" name="b2" value="Elimina Producto" onClick="borrar()">
  61. <table border="1" id="tabla_f2" bordercolor="#6B238E" align="center">
  62. <tr>
  63. <td><input type="submit" name="ok" id="ok" value="GUARDAR" /></td>
  64. </tr>
  65. </table>
  66. </form>
  67. </body>
  68. </html>
  69.  
  70.  
  71. <script language='JavaScript'>
  72. var cont=1;
  73. function addRowX()  //Esta la funcion que agrega las filas :
  74. {
  75. cont++;
  76. var indiceFila=1;
  77. myNewRow = document.getElementById('tabla').insertRow(-1);
  78. myNewRow.id=indiceFila;
  79. myNewCell=myNewRow.insertCell(-1);
  80. myNewCell.innerHTML='<td><input type="text" size="4" name="code_'+cont+'" /></td>';
  81. myNewCell=myNewRow.insertCell(-1);
  82. myNewCell.innerHTML='<select name="name_'+cont+'"/></select>';
  83. myNewCell=myNewRow.insertCell(-1);
  84. myNewCell.innerHTML='<input type="text" size="8" name="precio_'+cont+'"/>';
  85. myNewCell=myNewRow.insertCell(-1);
  86. myNewCell.innerHTML='<input type="text" size="8" name="cant_'+cont+'"/>';
  87. indiceFila++;
  88. }
  89.  
  90.  
  91. //////////////Borrar() ///////////
  92. function borrar() {
  93. var table = document.getElementById('tabla');
  94. if(table.rows.length > 3)
  95.     {
  96.     table.deleteRow(table.rows.length -1);
  97. cont--;
  98.     }
  99. }
  100.  
  101. ////////////FUNCION ASIGNA VALOR DE CONT PARA EL FOR DE MOSTRAR DATOS MP-MOD-TT////////
  102. function asigna()
  103. {
  104. valor=document.form.var_cont.value=cont;
  105. }
  106. </script>