Save variable after refresh php

Asked

Viewed 858 times

0

I have a code that saves the POST from an html form in the $ID variable, in case when this POST occurs the page changes due to this variable, so I would like to save it somehow so that I didn’t lose it when I refresh the page, so that the page doesn’t change.

  • You can store using session in that way $_SESSION['a'] = $ID

1 answer

3


To solve your "problem" I recommend that you save this variable in a session, or even in a cookie to be able to use it later.

If you wanted to save in a session, on the page that takes the data from $_POST, you could enter this code:

<?php
     session_start();//Colocar no inicio do código, se já não houver em alguma página que da include nessa
     //resto do código
     $id = $_POST['form'];
     $_SESSION['dados'] = $id;
     //resto do código
?>

Now to recover that data on any other page, you do the following:

<?php
     session_start();//Colocar no inicio do código, se já não houver em alguma página que da include nessa
     //resto do código
     $id = $_SESSION['dados'];
?>

Remembering that by being SESSION, as soon as the browser is closed, this data will be "removed" from SESSION.

If you want to know about Cookies, this that page.

  • $_SESSION['data'] = $id; what would be the data' ?

  • It is the name you are giving for the data that will be stored in this session, as well as an associative vector, in the Session you can put a "name" for the position where you will be saved something. The name 'data' was a generic title that I chose, just so I could access the stored content in the future. Let’s say I will store the email "[email protected]" in Session, in which case I would put: $_SESSION['email'] = '[email protected]';

  • in case I’m doing it on the same page, then I should put only a session_start(); at the right beginning?

  • Yes, most correct.

Browser other questions tagged

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