If the request is handled in the same file, Marcos's response , where it is used directly variable $_POST
, is valid. If it is necessary to treat the request in different files, I believe that session
is not the best output, but cookie
. Although they seem 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 to persist for a while some data that is interesting to the application in question, the difference is that session
persists the data on the server side and cookies
side of the user. Since we are working with a form, in which the user will provide the data itself, there is no risk to the application persisting the data on the user side . For the server, it only matters the data when they are already valid.
A discussion of this can be read here:
In this way, your form can be defined following the same logic presented in the other response, but now replacing the variable $_POST
with $_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
.