Hola. qué tal
Estoy desarrollando una tabla en la cual tengo un combo con un evento onchange en php y trato de hacerla dinámica con javascript, utilizo el código que han publicado en este post. El problema es que aún no logro hacerlo funcionar y debido a esto he decido solicitar su valiosa ayuda, de antemano les agradezco.
Código PHP:
<HTML>
<HEAD>
<script type="text/javascript" src="livevalidation_standalone.js"></script>
<TITLE> <- OTRO -> </TITLE>
<script language="javascript">
function addRowToTable()
{
var tbl = document.getElementById('tabla');
var lastRow = tbl.rows.length;
// if there's no header row in the table, then iteration = lastRow + 1
var iteration = lastRow;
var row = tbl.insertRow(lastRow);
// Combo nombre producto
var celdaComboNombreProducto = row.insertCell(0);
var sel = document.createElement('select');
sel.name = 'nombre_producto'+ iteration;
sel.id = 'id_nombre_producto'+ iteration;
sel.options[0] = new Option('Seleccionar Material', '0');
// Comentado por que lo cargariamos con un bucle while desde BBDD
<?php include('conecta.php');
$sqltarifa=mysql_query("select * from Material");
while ($ftarifa=mysql_fetch_array($sqltarifa)){
echo "sel.options['$ftarifa[0]']= new Option ('$ftarifa[1]','$ftarifa[3]');";
}
mysql_free_result($sqltarifa);
?>
<!--sel.onchange = function(){muestraPrecioProducto(this.value);}
sel.onchange = function(){muestraPrecioProducto(this.value, this.id);}
function muestraPrecioProducto(valor, id) {
document.getElementById(id+ '_precio').value = valor;
}
celdaComboNombreProducto.appendChild(sel);
// input precio producto
var celdaPrecioProducto = row.insertCell(1);
var el = document.createElement('input');
<!-- el.setAttribute("id", "uniqueIdentifier");
el.type = 'text';
el.id = 'id_precio_producto'+ iteration;
el.name = 'precio_producto'+ iteration;
el.size = 4;
alert (iteration);
<!--el.onkeypress = keyPressTest;
celdaPrecioProducto.appendChild(el);
}
</script>
<table id="tabla">
<form>
<tr>
<th colspan="7">Tabla Ejemplo</th>
</tr>
<?php
$sqlu="SELECT materialr3, descripcionmat, existencia, unidad from Material INNER JOIN Unidadmedida
ON umed=idum order by descripcionmat asc";
$respu=mysql_query($sqlu) or die ("Error al selccionar Unidad de Medida 1 ".mysql_error());
$dr=rand(); echo "\n $dr";
?> <INPUT type="button" value="Add Row" onclick="addRowToTable(tabla)" />
<TR>
<TD>
<select name="materialu" id="selecprod" onchange="r3d<?php echo "$dr"; ?>.value=this.options[this.selectedIndex].getAttribute('r3');
umedd<?php echo "$dr"; ?>.value=this.options[this.selectedIndex].getAttribute('medida');
exisd<?php echo "$dr"; ?>.value=this.options[this.selectedIndex].getAttribute('exis');"/>
<option value="$row['materialr3']">Seleccionar Material</option>
<?php
while ($row = mysql_fetch_assoc($respu)){ //while($row=mysql_fetch_row($respM))
?>
<option value='$row['descripcionmat'] r3="<?php echo $row['materialr3'] ?>
"medida="<?php echo $row['unidad'] ?>"exis="<?php echo $row['existencia'] ?>"> <?php echo $row['descripcionmat'] ?> </option>
<?php
}
?>
</select>
</TD>
<td><input type='text' name='r3[]' id='r3d<?php echo "$dr"; ?>' value='' size='4' maxlength='20' readonly></td>
<td><input type='text' name='exis[]' id='exisd<?php echo "$dr"; ?>' value='' size='5' maxlength='10' readonly></td>
<script>
var ex =document.getElementById("exisd").value;
var existencia = Math.abs(ex);
</script>
<td><input type='text' name='medida[]' id='umedd<?php echo "$dr"; ?>' value='' size='6' maxlength='10' readonly></td>
<td><input type='text' name='despachar[]' size='6' id='desc<?php echo "$dr"; ?>'></td>
<script type="text/javascript">
var d2 = new LiveValidation('desc');
d2.add(Validate.Presence);
d2.add( Validate.Numericality, { minimum: 1, onlyInteger: true } );</script>
<td><input type='text' name='destino[]' maxlength='21'></td>
<td><input type='text' name='obs[]' size='10'></td></tr>
</table>
</form>
</html>