What technique do you use to keep fields in a form filled or selected after $ _POST []? [duplicate]

8

I'm developing a real estate system and would like to ask how can I keep data for a form such as inputs , selects and checkbox selected after giving $_POST on the form.

I do not know how to store this data for all the navigation of that client that is in the site but I imagine that something must be related to sessions .

Thanks for the help.

    
asked by anonymous 18.11.2015 / 18:06

4 answers

6

Small examples demonstrating how it can be done:

COOKIE:

  

PHP transparently supports HTTP cookies. Cookies is a mechanism for storing data in the remote browser and allows for the tracing or identification of user feedback. You can create cookies using the setcookie () or setrawcookie () function. Cookies are a part of the HTTP header, so setcookie () must be called before any other data is sent to the browser. This is the same limitation that the header () function has. You can use the buffered output functions to delay script impressions until you have decided whether or not to set any cookie or send any headers.

Form:

<form action="server.php" method="POST">
    <?php
        if (isset($_COOKIE["valor"])){
            echo '<input type="text" name="valor" value="'.$_COOKIE["valor"].'" />';
        } else {
            echo '<input type="text" name="valor" value="" />';
        }
     ?>
</form> 

Server:

<?php
   $valor = $_POST['valor'];
   setcookie("valor",$valor);
?>

SESSION:

  

Sessions support in PHP consists of one way of preseeding data through subsequent accesses. This allows you to create more personalized applications and increase the appeal of your web site.

Form:

<form action="server.php" method="POST">
    <?php
        session_start();
        if (isset($_SESSION["valor"])){
             echo '<input type="text" name="valor" value="'.$_SESSION["valor"].'" />';
        } else {
             echo '<input type="text" name="valor" value="" />';
        }
    ?>
</form> 

Server:

<?php

    session_start();
    $valor = $_POST['valor'];
    $_SESSION['valor'] = $valor;

?>

Differences:

  

Cookie - The information is stored on your computer, an example of this and a login system for a discussion forum, if you enter it today, if you enter, you will still be logged in.

     

Session - Information is saved to the server. If you visit, visit a website, and leave, your session ends, or it may time out.

References:

link

link

link

    
18.11.2015 / 18:36
6

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);
}
    
18.11.2015 / 19:52
2

I do not know if this is what you want, but you can make your page have 2 types of behavior, a default and one for $ _POST requests, and when submitting your form, send it to the page itself. >

I usually use this to do validations, and when something is invalid, I can continue with the data that the user previously entered without needing to use AJAX to do the validation.

If you want to go to other pages and not lose this information, it is better to use cookies.

I do not think you need to use sessions.

    
18.11.2015 / 20:35
1

Just to complement the answers, in frameworks like Laravel 4 you can do this as follows:

No controller

return Redirect::back()->withInput();

In view:

{{ Form::text('name', Input::old('name') }}

Explanation

When you redirect by calling the withInput method, you are saving the content of the current $_POST to a flash (values in the session that are only displayed once.)

The Input::old method is in charge of capturing these values.

So my recommendation is that if you are not using any framework, use the session to save these values. Do this in a way that you can create as a flash, to ensure that the values, once used, will be removed at the same time as the session.

    
24.11.2015 / 18:03