Multi-step form, losing values of variables when page changes

Asked

Viewed 666 times

9

We have three PHP pages with html form. In the first page we have the fields showing the values of the variables and the fields Hidden containing the values of the variables like this:

<input name="nome" type="hidden" value=".$nome." >

From the first page to the second, all values are passed correctly via $_POST['nome'] that way, because I checked with echo var_dump($nome). From the second page to the third page which is the processing of the completed form whose values of the Hidden fields go to the database, the values of these variables are lost. The var_dump() shows that the variables were null (NULL) and I also get the message:

Indefined index [...]

Note that variables that only exist on the second page are normally inserted in the bank.

If I pass directly without field hidden, goes to the database the value .$nome.

What could I be doing wrong? How else could I advance these variables to the third page?

  • 1

    Look friend this practice that you are using to return the variables to the frontend and re-assimilate to the next pages is very insecure and flawed as you said yourself. I recommend you work with $_SESSION PHP, can read more here, store only once your variable in the session and you don’t need to put it in hidden fields for the next.

  • Uhmm... So is there such a flaw? Isn’t it necessarily my mistake? Okay, thank you!

  • 2

    No @Vinicius, what he meant is that the way he’s doing it is susceptible to crashes, not necessarily a PHP bug or glitch. It was probably your mistake at some point.

  • Note: var_dump() does not need to echo before him

1 answer

3


To work on a form with multiple steps/screens, it is advisable that you use the sessions, in PHP you can use through the global variable $_SESSION.

So you don’t need to each form keep sending and recovering data from one page to another. Not to mention that you don’t have to keep adding records to the database on each page, you can add all the records at the same time in the last step, so it’s easier to handle the data, easier to maintain and the code is even more readable.

Creating sessions:

You can set the data like this:

$_SESSION['formulario']['nome']  = $_POST['nome']
$_SESSION['formulario']['email'] = $_POST['email']

Recovering sessions:

On the next page you can recover this data by doing so:

$_SESSION['formulario']['nome']

You can recover the data anywhere and on any of the pages from now on. It’s much easier to work like this. Forget having to put the input hidden, That’s not necessary here.

Don’t forget to use session_start() to log in to the pages you will use.

  • 1

    I also thought about the lack of control that appears to be, because if $_SESSION is anywhere, then we can also have forms very similar to fields of the same name. By your example we can then specify from which form we are getting the information. What passes more security and less doubt (for example: where this value is coming from). Thanks! I will use more $_SESSION from now on.

  • 1

    Yes, while running your system, you can add various types of sessions, even user preferences, trackear user navigation, where it clicked, various things. Be sure to use.

  • @Ricardo it is good practice to use $_SESSION even when we have many users at the same time?

  • 1

    Yes, the session is only used for the current user, not shared with other users.

Browser other questions tagged

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