MANTENER VALORES DE UN FORM AL RECARGARLO
El problema típico de autocargar un form para operar sobre los datos es que los valores del form se pierden,
una forma de mantenerlos es con jquery.
Primero chequeamos con if(isset($_GET['accion'])) si venimos de recargar el form, luego de
recuperar los valores POR $_FORM por PHP Los reponemos por jquery usando echo
para pasar los valores de PHP a javascript.
Ver en el código como recuperar un select, un input, un radio button y un checkbox, porque en javascript
cada uno tiene una función distinta.
<head>
<?
$Ae="";
if(isset($_GET['accion'])){ //vuelvo de ejecutar
$us= $_POST["usuarios"];
$algo= $_POST["algo"];
$sel=$_POST["usuarios"];
$radio =$_POST["orden"];
if(isset($_POST['condiciones'])){
$ch = "ACEPTO";
$Ae="checked";
}else{
$ch = "NO ACEPTO";
$Ae="";
}
}else{
$us= "";
$algo= "";
$sel="";
$radio = "";
$ch = "";
}
?>
<script type="text/javascript">
$(document).ready(function(){
//mantener select list
$('#user option[value="<? echo "$sel" ?>"]').attr('selected', 'selected');
//mantener input
$('#texto').val("<? echo "$algo" ?>");
//mantener checkbox
$("#acepta").prop("checked", "<? echo $Ae;?>");
//mantener radio option
$('input:radio[value="<? echo "$radio" ?>"]').prop('checked', 'checked');
});
</script>
</head>
<body>
<form action="?accion=ejecutar" method="post" id="formulario">
<div style="margin: 0 auto; width:800px; padding-top:20px;">Elegir un usuario
<select name="usuarios" id="user" style=" width:150px; height:18px; margin-top:4px; font-size:12px; padding:0px;">
<option value="TODOS">Todos los usuarios </option>
<option value="Felisa">Felisa</option>
<option value="Juan">Juan</option>
<option value="Fernanda">Fernanda</option>
<option value="Adrián">Adrián</option>
<option value="Diana">Diana</option>
<option value="Carlos">Carlos</option>
</select>
escribir algo
<input name="algo" type="text" style="margin-left:10px;" value="" size="10" maxlength="20" id="texto" />
</div>
<div style="margin: 0 auto; text-align:left; margin-top:50px;">
¿Acepta las condiciones?
<input id="acepta" name="condiciones" type="checkbox" value="ACEPTO" checked />
</div>
<div style="margin-top:20px; ">
<table>
<tr>
<td width="100">Elegir:</td>
<td width="100">Fecha: <input type="radio" name="orden" value="fecha" checked="checked" /> </td>
<td width="100">Nombre: <input type="radio" name="orden" value="nombre" /></td>
<td width="100">Creador: <input type="radio" name="orden" value="creador" /></td>
</tr>
</table>
</div>
<div style="margin-top:20px; ">
<input type="submit" name="Submit" value="Ejecutar" style=" width:70px; height:18px; margin-top:4px; margin-bottom:4px; font-size:12px;" />
</div>
<div style="margin-top:20px; border:#000000 solid 1px; width:300px; padding:5px; ">
<?
echo $us . "<br><br>";
echo $algo . "<br><br>";
echo $radio . "<br><br>";
echo $ch;
?>
</div>
</form>
</body>