0

I understand there's "POST" and "GET" to obtain data from a HTML page. Is there a way to obtain the variable from the HTML page, save it on the PHP page as another variable so the variable will still be there even though the PHP page is being refreshed.

Right now I can present the variable on the PHP page, but after I refresh the page it doesn't save it or if I open a separate tab using the same URL, the variable isn't there.

Thanks.

3
  • 4
    Have a look at php.net/manual/en/features.sessions.php Commented Mar 14, 2017 at 20:13
  • 1
    you need to persist said value somewhere, either a database/session var, in the backend, or using localStorage/sessionStorage/indexedDB in the frontend Commented Mar 14, 2017 at 20:14
  • I'm not understed question, maybe php.net/manual/en/language.variables.variable.php Commented Mar 14, 2017 at 20:17

1 Answer 1

1

You can store HTML form variables to PHP session variables. Here is a super basic example:

<?php
session_start(); // this needs to be called anytime your working with sessions (even before destoying them)

if(isset($_POST['your_form_variable'])) {
    $_SESSION['your_session_variable'] = $_POST['your_form_variable'];
}

// $_SESSION['your_session_variable'] will now persist through every page load 
// until you destroy it by using:
//
// unset($_SESSION['your_session_variable']);
// 
// and to destroy them all:
//
// session_start();
// session_destroy();
// $_SESSION = array();

if(isset($_SESSION['your_session_variable'])) {
    echo $_SESSION['your_session_variable'];
}
?>
<form method="post">
  <input name="your_form_variable" value="Some value" />
  <button>Sumbit form</button>
</form>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.