dames
dames

Reputation: 1481

Refresh page without losing the Post value

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

Upvotes: 9

Views: 51668

Answers (7)

Hemantha
Hemantha

Reputation: 353

Actually in html forms it keeps post data. this is valuble when you need to keep inserted data in the textboxes.

  <form> 
<input type="text" name="student_name" value="<?php echo 
isset($_POST['student_name']) ? $_POST['student_name']:'';    
?>">
</form>

Upvotes: 1

Cloudx
Cloudx

Reputation: 1

You can use file to save post data so the data will not will not be removed until someone remove the file and of-course you can modify the file easily

if($_POST['name'])
{
$file = fopen('poststored.txt','wb'); 
fwrite($file,''.$_POST['value'].'');
fclose($file);
} 

if (file_exists('poststored.txt')) {
$file = fopen('ipSelected.txt', 'r');
$value = fgets($file);
fclose($file);
}

so your post value stored in $value.

Upvotes: 0

Glyn Jackson
Glyn Jackson

Reputation: 8354

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']; }

Upvotes: 7

Nir Alfasi
Nir Alfasi

Reputation: 53525

You can use the same value that you got in the POST inside the form, this way, when you submit it - it'll stay there.

An little example:

<?php
$var = mysql_real_escape_string($_POST['var']);
?>
<form id="1" name="1" action="/" method="post">
<input type="text" value="<?php print $var;?>"/>
<input type="submit" value="Submit" />
</form>

Upvotes: 0

WatsMyName
WatsMyName

Reputation: 4478

put post values to session

session_start();
$_SESSION["POST_VARS"]=$_POST;

and you can fetch this value in another page like

session_start();
$_SESSION["POST_VARS"]["name"];
$_SESSION["POST_VARS"]["address"];

Upvotes: 0

ronalchn
ronalchn

Reputation: 12335

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

Upvotes: 2

user1317647
user1317647

Reputation:

You could save your $_POST values inside of $_SESSION's Save your all $_POST's like this:

<?php
session_start();
$_SESSION['value1'] = $_POST['value1'];
$_SESSION['value2'] = $_POST['value2'];
// ETC...
echo "<input type='text' name='value1' value='".$_SESSION['value1']."' />";
echo "<input type='text' name='value2' value='".$_SESSION['value2']."' />";
?>

Upvotes: 1

Related Questions