If the request is handled in the same file, the reply of Marcos, where the variable is directly used $_POST
, is valid. If there is a need to process the request in separate files, I believe that session
is not the best way out, but yes cookie
. Although they appear to be the same, the purpose is different. HTTP requests are characterized as stateless, because data does not persist between multiple requests, as soon as the response to the request is obtained the data is lost. The goal of session
and cookie
is exactly persist for a while some data that are interesting to the application in question, the difference is that the session
persists the data on the server side and the cookies
persist the user-side data. Since we are working with a form, in which the user himself will provide the data, does not present any risk to the application persist the data on the user side. For the server, it only matters the data when it is already valid.
A discussion about it can be read here:
In this way, your form can be defined by following the same logic presented in the other answer, but now replacing the variable $_POST
for $_COOKIE
:
<input type="text" value="<?= isset($_COOKIE['form_foo']['nome']) ? $_COOKIE['form_foo']['nome'] : '' ?>" name="nome">
And in the file that handles the requests, after validating the data, persist them through the function setcookie
.
It doesn’t have to be $_SESSION, just put the $_POST or $_GET, whatever you have using, on the same form page.
– viana
In fact SESSION try to be gambiarra (depending on the case and the technique, even the to use can be identified only). If the form is in the same request, the answer below serves. If it is not, you need to direct the person with some ID in the URL and recover from somewhere (DB, DB in memory, temporary file etc);
– Bacco