For this answer I think you want the values "selected after giving a $_POST
on the form", so it is not necessary to keep this data in the session, only on the page submitted by the form.
There is no way for all field types, so I suggest having functions for each type, below I describe examples of functions for the text and select types, which can serve as a basis for other types.
I used the sprintf()
family functions to assemble the HTML, could concatenate, for example . But it goes from every developer.
To get the POST values I used filter_input()
, which does some validation, 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);
}