Parte1 Formulario
Código HTML :
<form id="gencampo" name="gencampo" action="<?php echo $gencampo['precuntas.php'];?>" method="post" onsubmit="asigna()"> <input type="text" name="pregunta" size="50" value=""> <br> <table border="0" id="tabla" cellspacing="1"> <tr colspan="13" align="left"> <td align="center"><font color="1166FF" size="3"><b></b></font></td> </tr> <tr align="left"> <td valign="top"><input type="button" name="b1" value="Agregar" onClick="camponuevo()"> <input type="button" name="b2" value="Quitar" onClick="borrar()" > <input type="submit" name="b3" id="b3" value="Guardar"></td> <td><font color="blue" size="1">Respuesta</font></td> <td><font color="blue" size="1">Opcion Generada</font></td> </tr> <tr> <td><input type="text" size="50" name="respuesta"></td> <input type="hidden" name="var_cont"> </tr> </table> </form>
Parte 2 Agregar o Borra campos nuevos
Código Javascript :
var cont=0; function camponuevo() //Esta la funcion que agrega los campos : { cont++; var indiceFila=1; campof = document.getElementById('tabla').insertRow(-1); campof.id=indiceFila; campo=campof.insertCell(-1); campo.innerHTML='<td><input type="text" size="50" name="respuesta"></td>'; campo=campof.insertCell(-1); campo.innerHTML='<td><input type="radio" name="opc"></td>'; indiceFila++; } function borrar() { var table = document.getElementById('tabla'); if(table.rows.length > 3) { table.deleteRow(table.rows.length -1); cont--; } }
Parte 3 Asigna y Muestra el resultado
Código Javascript :
function borrar() { var table = document.getElementById('tabla'); if(table.rows.length > 3) { table.deleteRow(table.rows.length -1); cont--; } } function asigna() { valor=document.gencampo.var_cont.value=cont; //alert('funcion asigna, valor cont:'+valor) <? $pregunta=$_POST["pregunta"]; $resp=$_POST["respuesta"]; $opcion=$_POST["opc"]; ?> }
Código PHP :
<? echo "</br>Pregunta $i: $pregunta<br>"; for ($i=1;$i<=$_POST["var_cont"];$i++) { echo "</br>Respuesta: $i $resp<br>"; } echo "</br>La respuesta correcta es: $opcion"; ?>
Algunas soluciones que se me han ocurrido son estas:
Código PHP :
<input type="text" size="50" name="respuesta_'+con+'"> $resp=$_POST['respuesta_con']; for ($i=1;$i<=$_POST["var_cont"];$i++) { echo "</br> Respuesta $i: ." ". $resp<br>"; }
Pero solo me muestra el id $i y el numero de textbox que corresponde a con.
Estaba pensando implementar un array y utilizar un while, pero no estoy seguro de como aplicarlo se me ocurre algo así:
Código PHP :
reset($var1); while (list($clave, $valor) = each($var1)) { echo "</br> $valor <br>""; }
Nesecito algo de ayuda!
