Use header("Location ./") without losing form content

Asked

Viewed 1,053 times

1

I am working with a POST method form and if the user sends it with some field filled incorrectly I want it to go back to the page but without losing the previously filled content.

I intended to use header("Location ./"), but I don’t know if this will keep what was already filled. And using GET method is not an option.

  • And where is the code for your question?

  • This is not what Location is for. If you want to persist with the data, simply process it in the PHP that receives the form. Use Location only in case of success.

1 answer

1

One approach is that the page in the action of your form, return the form itself with the presence of the error information (which fields should be modified by the user) and with the inputs filled in. For example, you have a file form.php and another call verifica.php:

php form.

<!--Verifica se tem alguma mensagem de erro
(quando incluido pelo arquivo verifica.php (require_once))
-->
<?php
    //verifica se existe a variavel $mensagemErros
    //ela só vai existir se o arquivo form.php
    //for incluido pelo arquivo verifica.php
    if(isset($mensagemErros)){
        echo '<div>' . $mensagemErros . '</div>';
    }
?>

<!--No formulario basta checar o array $_POST para exibir 
alguma informação submetida anteriormente-->
<form method="post" action="verifica.php">
    <input type="text" name="a" 
    value="<?php echo isset($_POST['a']) ? $_POST['a'] : '';?>">
    <input type="text" name="b"
    value="<?php echo isset($_POST['a']) ? $_POST['b'] : '';?>">
    <input type="submit" name="enviar_form">
</form>

php checks.

<?php

//verifica se o formulario foi enviado
if(isset($_POST['enviar_form'])){
    //verifica se tem erros (alguma função feita por você)
    $temErros = true;

    if($temErros){
        $mensagemErros = "Foram encontrados os seguintes erros: ...";
        require_once './form.php';  
        //ao chamar o arquivo form.php, o array global $_POST
        //estara disponivel, sendo possivel alterar o atributo
        //value de cada input, com os dados submetidos pelo usuario
    }else{
        //vai para outro lugar
        header('Location: index.php');  
    }
}

//nada mais deve ser impresso
//para não se "misturar" com o html do formulário

The user initially accesses the file form.php, when the form is submitted, the page will be called verifica.php, if there is a validation error, verifica.php, return the file form.php, showing the error information and inputs with the previously submitted values.

  • But they are not error messages that the AP wants to display, but rather keep the form filled, so as not to have to force the user to reprimand it.

  • In a way it encompasses both, because then the user can know which field should be edited. Maybe I haven’t made it clear that both are done: filling out the form with the previous data and indicating which fields need to be changed.

  • Ah, yes, it had read wrong. Indeed it will fill.

  • I will edit the reply, because rereading now, I saw that gives to imply that only error messages are displayed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.