473,386 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Clearing POST DATA for Browser Refresh

PHP Pros:

I have a simple html form that submits data to a php script, which
processes it, and then redisplays the same page, but with a "thank you"
message in place of the html form. This is all working fine. However,
when refresh the browser, I get the following message displayed:

"The page you are trying to view contains POSTDATA. If you resend the
data, any action the form carried out (such as as search or online
purchse) will be repeated. To resend the data, click OK. Otherwise,
click Cancel."

Obviously I don't want my users to resend the data to me.

What do I need to do code-wise so that when the browser is refreshed,
the page is reloaded without this message being displayed.

Thanks in advance.
Feb 14 '06 #1
15 33175
tmax wrote:
PHP Pros:

I have a simple html form that submits data to a php script, which
processes it, and then redisplays the same page, but with a "thank you"
message in place of the html form. This is all working fine. However,
when refresh the browser, I get the following message displayed:

"The page you are trying to view contains POSTDATA. If you resend the
data, any action the form carried out (such as as search or online
purchse) will be repeated. To resend the data, click OK. Otherwise,
click Cancel."

Obviously I don't want my users to resend the data to me.

What do I need to do code-wise so that when the browser is refreshed,
the page is reloaded without this message being displayed.

Thanks in advance.

Don't redirect to the form; redirect to a summary page without the form
or make your form page sensitive to whether the $_POST data is available
and do not have it emit a <form> if the $_POST data is available.

I prefer the first method over the second.

-david-

Feb 14 '06 #2
d
"tmax" <tm**@comcast.net> wrote in message
news:82***************************@PGTV.COM...
PHP Pros:

I have a simple html form that submits data to a php script, which
processes it, and then redisplays the same page, but with a "thank you"
message in place of the html form. This is all working fine. However,
when refresh the browser, I get the following message displayed:

"The page you are trying to view contains POSTDATA. If you resend the
data, any action the form carried out (such as as search or online
purchse) will be repeated. To resend the data, click OK. Otherwise, click
Cancel."

Obviously I don't want my users to resend the data to me.

What do I need to do code-wise so that when the browser is refreshed, the
page is reloaded without this message being displayed.

Thanks in advance.


In the script to which the form submits, store the $_POST data somewhere
else, say a session. Then, use this:

header("Location: http://host/path/to/script.php");
exit();

to bounce the user to another page. Most browsers know to not re-submit the
post data to this new page. The new script can even be the originating
one - as long as you use the location: header, you'll be fine. You can then
access the submitted data wherever you stored it.

PS. if you're using a session to store the data, call session_write_close()
before the header() call, otherwise nasty things happen on some browsers.

dave
Feb 14 '06 #3

d wrote:
"tmax" <tm**@comcast.net> wrote in message
news:82***************************@PGTV.COM...
PHP Pros:

I have a simple html form that submits data to a php script, which
processes it, and then redisplays the same page, but with a "thank you"
message in place of the html form. This is all working fine. However,
when refresh the browser, I get the following message displayed:

"The page you are trying to view contains POSTDATA. If you resend the
data, any action the form carried out (such as as search or online
purchse) will be repeated. To resend the data, click OK. Otherwise, click
Cancel."

Obviously I don't want my users to resend the data to me.

What do I need to do code-wise so that when the browser is refreshed, the
page is reloaded without this message being displayed.

Thanks in advance.


In the script to which the form submits, store the $_POST data somewhere
else, say a session. Then, use this:

header("Location: http://host/path/to/script.php");
exit();

to bounce the user to another page. Most browsers know to not re-submit the
post data to this new page. The new script can even be the originating
one - as long as you use the location: header, you'll be fine. You can then
access the submitted data wherever you stored it.

PS. if you're using a session to store the data, call session_write_close()
before the header() call, otherwise nasty things happen on some browsers.

dave


Just curious, but is there / would you recommend a good alternative for
the Sessions?

Frizzle.

Feb 14 '06 #4
Well, I've tried everything that has been suggested so far - thanks for
everyone's input.

However, let me restate the problems differently as I believe it wasn't
correct to begin with.

I have a file: index.php

index.php contains a form, which when submitted for processing, calls
itself using <form method="POST" action="{$_SERVER['PHP_SELF']}">

When the form is submitted and index.php is reevaluated, I have the
following IF statement: (FYI: 'submit' is the name of the submit button.)
------------
IF ( !isset($_POST['submit']) ) {
// The form has NOT been SUBMITTED - display the form

echo <<<htmloutput
<form method="POST" action="{$_SERVER['PHP_SELF']}">
...
</form>
htmloutput;

} ELSE {
// The form was SUBMITTED - process the form

process the form data, etc. and print in PLACE of the form the
following:

echo "Information submitted... Thank you.";
}
-------------

I understand I can redirect to a (different) summary page, but all I
want to do is replace the form with the summary message. The problem
comes into being when, after the summary message is displayed, the user
goes to refresh the browser. The browser is still caching the form data
for resubmission. I've tried things like: unset($_POST) or
unset($_POST['submit']) (and all the other form fields) but that has no
affect.

Any other suggestions?

Thanks in advance.

d wrote:
"tmax" <tm**@comcast.net> wrote in message
news:82***************************@PGTV.COM...
PHP Pros:

I have a simple html form that submits data to a php script, which
processes it, and then redisplays the same page, but with a "thank you"
message in place of the html form. This is all working fine. However,
when refresh the browser, I get the following message displayed:

"The page you are trying to view contains POSTDATA. If you resend the
data, any action the form carried out (such as as search or online
purchse) will be repeated. To resend the data, click OK. Otherwise, click
Cancel."

Obviously I don't want my users to resend the data to me.

What do I need to do code-wise so that when the browser is refreshed, the
page is reloaded without this message being displayed.

Thanks in advance.

In the script to which the form submits, store the $_POST data somewhere
else, say a session. Then, use this:

header("Location: http://host/path/to/script.php");
exit();

to bounce the user to another page. Most browsers know to not re-submit the
post data to this new page. The new script can even be the originating
one - as long as you use the location: header, you'll be fine. You can then
access the submitted data wherever you stored it.

PS. if you're using a session to store the data, call session_write_close()
before the header() call, otherwise nasty things happen on some browsers.

dave

Feb 14 '06 #5
I have to ask, why would you assume your users would refresh that page
rather than following a link somewhere else?

Feb 14 '06 #6
I have to ask, why would you assume your users would refresh that page
rather than following a link somewhere else?

Feb 14 '06 #7
I don't think there is another solution than storing state information
somehwere.
I would use the following method:

1. Before displaying the form, generate some unique number
2. add this number in a hidden field in the form.
3. When the user submits, mark the number as 'used'
4. When the user submits again, ignore the data, because the number
has already been 'used' up.

The simplest way to do this is to create a counter, either in a file on disk
or in
a database. Before you send the form to the client, increment the counter
and send
the counter value in a hidden field in the form.

If you save the form data in a database table, add a 'counter' field to the
table.
When the form is first submitted, a record with the counter value sent with
the form should not exist. Save data and counter.
If the form is submitted for a second or third time, the counter is already
present in
your table, so you should ignore the data in the post.

Using a session cookie is, I think, less work.
"tmax" <tm**@comcast.net> wrote in message
news:82***************************@PGTV.COM...
PHP Pros:

I have a simple html form that submits data to a php script, which
processes it, and then redisplays the same page, but with a "thank you"
message in place of the html form. This is all working fine. However,
when refresh the browser, I get the following message displayed:

"The page you are trying to view contains POSTDATA. If you resend the
data, any action the form carried out (such as as search or online
purchse) will be repeated. To resend the data, click OK. Otherwise, click
Cancel."

Obviously I don't want my users to resend the data to me.

What do I need to do code-wise so that when the browser is refreshed, the
page is reloaded without this message being displayed.

Thanks in advance.

Feb 14 '06 #8

True, they could instead following some other link and decide not to
refresh the page. However, I would like the page to function such that
refreshing the page simply re-displays a new blank form (after all,
refreshing a page is conceptually akin to resetting.)

I started off on this path thinking that this format should be
relatively easy to code with one if-else statement. So far, it has been
easy to code (the form and summary message work great, with very little
coding). I keep thinking that there is some PHP mechanism that isn't
being used properly here (due to my PHP inexperience.)

Any suggestions?

P-Rage wrote:
I have to ask, why would you assume your users would refresh that page
rather than following a link somewhere else?

Feb 14 '06 #9
d
"frizzle" <ph********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...

d wrote:
"tmax" <tm**@comcast.net> wrote in message
news:82***************************@PGTV.COM...
> PHP Pros:
>
> I have a simple html form that submits data to a php script, which
> processes it, and then redisplays the same page, but with a "thank you"
> message in place of the html form. This is all working fine. However,
> when refresh the browser, I get the following message displayed:
>
> "The page you are trying to view contains POSTDATA. If you resend the
> data, any action the form carried out (such as as search or online
> purchse) will be repeated. To resend the data, click OK. Otherwise,
> click
> Cancel."
>
> Obviously I don't want my users to resend the data to me.
>
> What do I need to do code-wise so that when the browser is refreshed,
> the
> page is reloaded without this message being displayed.
>
> Thanks in advance.
In the script to which the form submits, store the $_POST data somewhere
else, say a session. Then, use this:

header("Location: http://host/path/to/script.php");
exit();

to bounce the user to another page. Most browsers know to not re-submit
the
post data to this new page. The new script can even be the originating
one - as long as you use the location: header, you'll be fine. You can
then
access the submitted data wherever you stored it.

PS. if you're using a session to store the data, call
session_write_close()
before the header() call, otherwise nasty things happen on some browsers.

dave


Just curious, but is there / would you recommend a good alternative for
the Sessions?


Databases or files spring to mind :)
Frizzle.

Feb 14 '06 #10
d
"tmax" <tm**@comcast.net> wrote in message
news:1f***************************@PGTV.COM...
Well, I've tried everything that has been suggested so far - thanks for
everyone's input.

However, let me restate the problems differently as I believe it wasn't
correct to begin with.


I understand completely what you're asking :)

Try this code:

<?

session_start();

if (isset($_POST["data"])) {
$_SESSION["data"]=$_POST["data"];
session_write_close();
header("Location: ".$_SERVER["SCRIPT_URI"]);
exit();
}

if (isset($_SESSION["data"])) {
?>
<html>
<head><title>Thanks!</title></head>
<body>
Thank you for submitting your data:<br>
<?=$_SESSION["data"];?>
</body>
</html>
<?
} else {
?>
<html>
<head><title>Please Submit</title></head>
<body>
Use the form to submit your data:<br>
<form method="post">
<input type="text" name="data"><br>
<input type="submit" value="Go!">
</form>
</body>
</html>
<?
}
?>

It will show a form, and take that posted data and store it in a session.
Then, the script re-directs you back to itself, and that redirection stops
your browser wanting to resubmit the data when refreshed. The session is
then checked, and if the data is present, a thank-you note is displayed. If
not, it then shows the initial form.

dave
Feb 14 '06 #11
d
"tmax" <tm**@comcast.net> wrote in message
news:a3***************************@PGTV.COM...

True, they could instead following some other link and decide not to
refresh the page. However, I would like the page to function such that
refreshing the page simply re-displays a new blank form (after all,
refreshing a page is conceptually akin to resetting.)

I started off on this path thinking that this format should be relatively
easy to code with one if-else statement. So far, it has been easy to code
(the form and summary message work great, with very little coding). I
keep thinking that there is some PHP mechanism that isn't being used
properly here (due to my PHP inexperience.)

Any suggestions?
The header("Location: ") command :) See my other post... ;)
P-Rage wrote:
I have to ask, why would you assume your users would refresh that page
rather than following a link somewhere else?

Feb 14 '06 #12
Thank You Dave!!!

This code works - I used ob_start() to get around some header() errors,
but after implementing that, my form and page works exactly as I
intended it to. Learned quite a bit from all of this too. Thanks again!

Travis
d wrote:
"tmax" <tm**@comcast.net> wrote in message
news:1f***************************@PGTV.COM...
Well, I've tried everything that has been suggested so far - thanks for
everyone's input.

However, let me restate the problems differently as I believe it wasn't
correct to begin with.

I understand completely what you're asking :)

Try this code:

<?

session_start();

if (isset($_POST["data"])) {
$_SESSION["data"]=$_POST["data"];
session_write_close();
header("Location: ".$_SERVER["SCRIPT_URI"]);
exit();
}

if (isset($_SESSION["data"])) {
?>
<html>
<head><title>Thanks!</title></head>
<body>
Thank you for submitting your data:<br>
<?=$_SESSION["data"];?>
</body>
</html>
<?
} else {
?>
<html>
<head><title>Please Submit</title></head>
<body>
Use the form to submit your data:<br>
<form method="post">
<input type="text" name="data"><br>
<input type="submit" value="Go!">
</form>
</body>
</html>
<?
}
?>

It will show a form, and take that posted data and store it in a session.
Then, the script re-directs you back to itself, and that redirection stops
your browser wanting to resubmit the data when refreshed. The session is
then checked, and if the data is present, a thank-you note is displayed. If
not, it then shows the initial form.

dave

Feb 15 '06 #13
tmax wrote:
Thank You Dave!!!

This code works - I used ob_start() to get around some header() errors,
but after implementing that, my form and page works exactly as I
intended it to. Learned quite a bit from all of this too. Thanks again!

Travis
d wrote:
"tmax" <tm**@comcast.net> wrote in message
news:1f***************************@PGTV.COM...
Well, I've tried everything that has been suggested so far - thanks
for everyone's input.

However, let me restate the problems differently as I believe it
wasn't correct to begin with.


I understand completely what you're asking :)

Try this code:

<?

session_start();

if (isset($_POST["data"])) {
$_SESSION["data"]=$_POST["data"];
session_write_close();
header("Location: ".$_SERVER["SCRIPT_URI"]);
exit();
}

if (isset($_SESSION["data"])) {
?>
<html>
<head><title>Thanks!</title></head>
<body>
Thank you for submitting your data:<br>
<?=$_SESSION["data"];?>
</body>
</html>
<?
} else {
?>
<html>
<head><title>Please Submit</title></head>
<body>
Use the form to submit your data:<br>
<form method="post">
<input type="text" name="data"><br>
<input type="submit" value="Go!">
</form>
</body>
</html>
<?
}
?>

It will show a form, and take that posted data and store it in a
session. Then, the script re-directs you back to itself, and that
redirection stops your browser wanting to resubmit the data when
refreshed. The session is then checked, and if the data is present, a
thank-you note is displayed. If not, it then shows the initial form.

dave


His code should work fine as long as you don't have *anything* before
the first line - including blank lines, spaces and DOCTYPE statements.

You should not need ob_start().

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Feb 15 '06 #14
On 2006-02-14, tmax <tm**@comcast.net> wrote:
PHP Pros:

I have a simple html form that submits data to a php script, which
processes it, and then redisplays the same page, but with a "thank you"
message in place of the html form. This is all working fine. However,
when refresh the browser, I get the following message displayed:

"The page you are trying to view contains POSTDATA. If you resend the
data, any action the form carried out (such as as search or online
purchse) will be repeated. To resend the data, click OK. Otherwise,
click Cancel."

Obviously I don't want my users to resend the data to me.

What do I need to do code-wise so that when the browser is refreshed,
the page is reloaded without this message being displayed.

Thanks in advance.


I think this will work.
when they post the form issue a redirect
(use a fixed pitch font or the following won't make much sense)

user posts automatic user navigates
form -----> redirect ------> thankyou --------> further
page page

when the user goes back from the "further" page they'll hit the
thankyou page (which was got by the redirect so no data to post)
(back again gets them to the form.)
Bye.
Jasen
Feb 15 '06 #15
P-Rage:
I have to ask, why would you assume your users would refresh that page
rather than following a link somewhere else?


Browsers developed under a misconception of History lists ask you if
you want to resend POST data after, say, you follow a link away from
the page to which the form directed you, and then, for whatever reason,
move Back to that page. By HTTP, if the user has not reconfigured
their browser to refresh stale History items, this should not happen
(sec. 13.13). The solutions put forward in this thread, if serving no
other purpose, work around that bug.

--
Jock

Feb 16 '06 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: brendan | last post by:
Sorry this isnt a cross post .. i just didnt get any help from alt.php. I have a website which utilises post forms for navigation in some areas. Problem is, when *some* users hit the BACK button...
7
by: milkyway | last post by:
Hello to all ;-) I am using the Visual Studio 2005 .NET framework. Basically, I have file that is served up as an HTML file on the client side (generated from a .aspx file). But - the HTML...
1
by: David P. Donahue | last post by:
I have an ASP .NET website where users submit comments and, depending on whether or not the web service accepting the comments returns an error, the form uses this.page.registerstartupscript() to...
1
by: Dabbler | last post by:
What effect does the browser refresh button have on a) session variables and b) viewstate? My page loads info based on session variables and it seems this breaks when someone does a page...
4
by: Thomas Eichner | last post by:
Hi, does anybody know a public website which offers a service that displays all data send by a browser (or an app calling the website), especially HTTP GET and POST data, browser data etc.? I...
2
by: pingalkar | last post by:
In my application, on one form i m getting information from user and save this information , after saving this information again we loading that page. (At this moment if any one try to refresh this...
8
by: ninuhadida | last post by:
hi there! This is actually my first post over here.. I thought i'd register since most answers to my common problems that i google up turn up to be in this site.. however I have a problem myself now....
6
by: kiran Vidhate | last post by:
Hi I need small help, What I need is when I write a script to handle my post data on my login page. if login failed and i press refresh key browser shows msg to resend the post data I tried...
4
by: =?Utf-8?B?QXNhZg==?= | last post by:
Hi, How can I prevent from an ASP.NET page to resend all the data again when the user press the Refresh button or F5 on the browser? Thanks in advanced, Asaf
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.