Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refresh page without losing the Post value

Tags:

html

php

How do I maintain the $post value when a page is refreshed; In other words how do I refresh the page without losing the Post value

like image 916
dames Avatar asked Sep 02 '12 08:09

dames


People also ask

How do you keep value after page reloading?

When you need to set a variable that should be reflected in the next page(s), use: var someVarName = "value"; localStorage. setItem("someVarKey", someVarName);

Does a Post request refresh the page?

a) If you submitted a form, performing a POST and then hit refresh, the browser will do another POST. b) If you just clicked a link that took you to another page, performing a GET, you'll a refresh will perform a get.

What happens when a page is refreshed?

For example, if you are on a web page, refreshing the page displays the most recent content published on that page. Essentially, you're asking the site to send your computer the newest version of the page you're viewing. 2. The refresh button, also known as the refresh option, is a function of all Internet browsers.


2 Answers

This in not possible without a page submit in the first place! Unless you somehow submitted the form fields back to the server i.e. Without Page Refresh using jQuery etc. Somesort of Auto Save Form script.

If this is for validation checks no need for sessions as suggested.

User fills in the form and submits back to self Sever side validation fails

$_GET

    <input type="hidden" name="first" 
   value="<?php echo htmlspecialchars($first, ENT_QUOTES); ?>" />

validation message, end.

alternatively as suggested save the whole post in a session, something like this, but again has to be first submitted to work....

$_POST

if(isset($_POST) & count($_POST)) { $_SESSION['post'] = $_POST; }
if(isset($_SESSION['post']) && count($_SESSION['post'])) { $_POST = $_SESSION['post']; }
like image 134
Glyn Jackson Avatar answered Oct 12 '22 18:10

Glyn Jackson


You can't do this. POST variables may not be re-sent, if they are, the browser usually does this when the user refreshes the page.

The POST variable will never be re-set if the user clicks a link to another page instead of refreshing.

If $post is a normal variable, then it will never be saved.

If you need to save something, you need to use cookies. $_SESSION is an implementation of cookies. Cookies are data that is stored on the user's browser, and are re-sent with every request.

Reference: http://php.net/manual/en/reserved.variables.session.php

The $_SESSION variable is just an associative array, so to use it, simply do something like:

$_SESSION['foo'] = $bar
like image 33
ronalchn Avatar answered Oct 12 '22 17:10

ronalchn