I consider to this answer that you want the values "selected after giving a $_POST
in the form", therefore it is not necessary to keep this data in the session, only on the page submitted by the form.
There is no equal way for all field types, so I suggest having functions for each type, below describe examples of functions for text and select types, which can serve as basis for other types.
I used the functions of the sprintf()
to mount the HTML, could concatenate, for example. But it goes from each developer.
To get the POST values I used filter_input()
, that makes some validations, avoiding the use of empty()
or isset()
.
function inputTextComValor($nome_do_campo) {
vprintf('<input type="text" name="%s" value="%s"/>', array(
$nome_do_campo,
filter_input(INPUT_POST, $nome_do_campo), // equivale a $_POST[$nome_do_campo]
));
}
function selectComValor($nome_do_campo, $valores) {
$selecionado = filter_input(INPUT_POST, $nome_do_campo);
$opcoes = '';
foreach ($valore as $chave => $valor) {
$opcoes .= vsprintf('<option value="%s' %s>%s</option>, array(
$chave,
$chave == $selecionado ? 'selected' : '',
$valor,
));
}
printf('<select name="%s">%s</select>', $nome_do_campo, $opcoes);
}
Send with an AJAX to prevent page reloading and prevent it from being touched in the values of the HTML elements.
– Giancarlo Abel Giulian
By ajax at one time or another he goes home or to the contact page and loses the data.
– Marcos Vinicius
I get it, so you want when he goes back to the page the data is still there, even when he closes the browser? If yes you can use COOKIES, if not the use of SESSIONS would be the solution.
– Giancarlo Abel Giulian