Page 1 of 1
Can you empty POST data?
Posted: Sat Sep 24, 2005 7:35 am
by pilau
At some tutorial was written you can clear POSTDATA by doing $_POST = array();
However, I tried it on different servers and at any case FF said that I still have POST data when I hit "refresh".
So maybe it's not even possible?
Posted: Sat Sep 24, 2005 7:40 am
by Nathaniel
I think that to clear post data, you would probably want something similar to header("Location: $the_current_page");.
Posted: Sat Sep 24, 2005 7:44 am
by shiznatix
if you load a page by a form with post data in it then even on that page if you $_POST = array() when you hit refresh it will refresh the page as it was when you first got there, with $_POST data and all.
Posted: Sat Sep 24, 2005 7:48 am
by Maugrim_The_Reaper
POSTDATA is not PHP's $_POST
POSTDATA is sent from the browser to the server. Code on the server will not effect the client's behaviour in that regard.
Firefox after submitting data by POST will request if you want to so again - IF you refresh the page following a form submit. Actually this is one of the reasons I use GET for requests leading to an action (despite what Google Webaccelerator advocates may swear to).
Since you are refreshing, FF takes it you may also want to submit the data again / or not. Either way you get to choose rather than leaving it up a non-sentient browser

Posted: Sat Sep 24, 2005 9:08 am
by pilau
So the only way to clear POSTDATA is to use header()?
Posted: Sat Sep 24, 2005 9:11 am
by feyd
you have to jump to a new page request that doesn't use post, that's the only way to get rid of the post data (although it's still stored in the browsers memory)
Posted: Sat Sep 24, 2005 11:16 am
by BruceT
Just use
if you want to get rid of all of the data, or
to get rid of a specific element of the array.
Edit - the browser does still hold the contents in memory though, so if the page is refreshed, it will attempt to resubmit the data. I guess the only way to clear it for good is to load a separate 'clean' page.
Posted: Sat Sep 24, 2005 11:17 am
by feyd
that won't acutally get rid of the data bruce.
Posted: Sat Sep 24, 2005 12:16 pm
by pilau
Hmm, ok. Understood.
However, a new question had popped in front of my eyes: If I set a new element in the $_POST array, and then submit an HTML form, that is:
Code: Select all
<?php
$_POST['something'] = "string";
print "<form action='this.file' method ='POST'>
<input type='submit'>
</form>";
?>
Will POSTDATA contain the "something" element afterwards?
Posted: Sat Sep 24, 2005 12:27 pm
by Weirdan
pilau wrote:
Will POSTDATA contain the "something" element afterwards?
No.
To add a field to $_POST array after resubmit you have to regenerate your form with additional field(s):
Code: Select all
<?php
$_POST['something'] = "string";
print "<form action='this.file' method ='POST'>";
foreach($_POST as $key=>$val) {
print "<input type='hidden' name='$key' value='$val' />\n";
}
print "
<input type='submit'>
</form>";
?>
Posted: Sat Sep 24, 2005 1:52 pm
by pilau
Weirdan wrote:Code: Select all
<?php
$_POST['something'] = "string";
print "<form action='this.file' method ='POST'>";
foreach($_POST as $key=>$val) {
print "<input type='hidden' name='$key' value='$val' />\n";
}
print "
<input type='submit'>
</form>";
?>
That should be used if I want to generate the exact form I had before I refreshed the page.
(Which basically I could just copy the source code from the form I made

)
Posted: Sat Sep 24, 2005 11:34 pm
by AGISB
If all you want to archive is that you cannot refresh the post data you can use sessions to disallow the refresh.
You can set a session var on the form page and only allow post data to be shown if that var is correct. After the data is shown change that var. For the cookie disabled you can use sid or tell them to enable cookies to submit the form.
Posted: Sat Sep 24, 2005 11:48 pm
by s.dot
pilau wrote:Weirdan wrote:Code: Select all
<?php
$_POST['something'] = "string";
print "<form action='this.file' method ='POST'>";
foreach($_POST as $key=>$val) {
print "<input type='hidden' name='$key' value='$val' />\n";
}
print "
<input type='submit'>
</form>";
?>
That should be used if I want to generate the exact form I had before I refreshed the page.
(Which basically I could just copy the source code from the form I made

)
No, don't copy the source code.. because his example used hidden input fields. This would resubmit the former postdata along with post['something'] but be seamless to the end user.
Posted: Sun Sep 25, 2005 12:03 am
by josh
couldn't that be achieved much easier using sessions?
Posted: Sun Sep 25, 2005 9:10 am
by pilau
Maybe you could explain more about that session example?