Question on refresh 
August 21st, 2007, 02:15 PM
| | | Question on refresh
Here is a problem I have come across a few times and wonder if there is a
simple solution.
On my form I have a submit button called, say, addEntry. I do a test for it
as
if (isset($_POST[addEntry'])) { code to insert stuff into the database }
The problem is that after clicking the button, and the code returns it to
the same screen, if the user clicks the refresh button on the browser it
will add another entry. Is there a simple way to unset an html field test?
I know that unset can be unset a variable in php, but this is testing an
action on the form.
--
Shelly | 
August 21st, 2007, 02:25 PM
| | | Re: Question on refresh
On Tue, 21 Aug 2007 16:11:35 +0200, Shelly
<sheldonlg.news@asap-consult.comwrote: Quote:
Here is a problem I have come across a few times and wonder if there is a
simple solution.
>
On my form I have a submit button called, say, addEntry. I do a test
for it
as
>
if (isset($_POST[addEntry'])) { code to insert stuff into the database }
>
The problem is that after clicking the button, and the code returns it to
the same screen, if the user clicks the refresh button on the browser it
will add another entry. Is there a simple way to unset an html field
test?
| One of the most common ways to achieve this is to do the thing on a post,
without any output (unless possibly when there are errors), and then do a
header redirect to a page (the same or another, whichever is more
appropriate). POST values will be lost on a redirect, so this snippet will
not 're-enter' the information:
<?php
if(isset($_POST['submit'])){
//do your thing here
header('Location: http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
exit;
}
//displaying of page here
?>
--
Rik Wasmus | 
August 21st, 2007, 02:35 PM
| | | Re: Question on refresh
"Rik" <luiheidsgoeroe@hotmail.comwrote in message
news:op.txexy4dsqnv3q9@metallium... Quote:
On Tue, 21 Aug 2007 16:11:35 +0200, Shelly
<sheldonlg.news@asap-consult.comwrote:
> Quote:
>Here is a problem I have come across a few times and wonder if there is a
>simple solution.
>>
>On my form I have a submit button called, say, addEntry. I do a test
>for it
>as
>>
>if (isset($_POST[addEntry'])) { code to insert stuff into the database }
>>
>The problem is that after clicking the button, and the code returns it to
>the same screen, if the user clicks the refresh button on the browser it
>will add another entry. Is there a simple way to unset an html field
>test?
| >
One of the most common ways to achieve this is to do the thing on a post,
without any output (unless possibly when there are errors), and then do a
header redirect to a page (the same or another, whichever is more
appropriate). POST values will be lost on a redirect, so this snippet will
not 're-enter' the information:
>
<?php
if(isset($_POST['submit'])){
//do your thing here
header('Location: http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
exit;
}
//displaying of page here
| Wow! It is as simple as that? Simply redirect the page to itself after the
insertion into the database? Thanks.
Shelly | 
August 21st, 2007, 02:35 PM
| | | Re: Question on refresh
On Aug 21, 10:17 am, Rik <luiheidsgoe...@hotmail.comwrote: Quote:
On Tue, 21 Aug 2007 16:11:35 +0200, Shelly
>
<sheldonlg.n...@asap-consult.comwrote: Quote:
Here is a problem I have come across a few times and wonder if there is a
simple solution.
| > Quote:
On my form I have a submit button called, say, addEntry. I do a test
for it
as
| > Quote: |
if (isset($_POST[addEntry'])) { code to insert stuff into the database }
| > Quote:
The problem is that after clicking the button, and the code returns it to
the same screen, if the user clicks the refresh button on the browser it
will add another entry. Is there a simple way to unset an html field
test?
| >
One of the most common ways to achieve this is to do the thing on a post,
without any output (unless possibly when there are errors), and then do a
header redirect to a page (the same or another, whichever is more
appropriate). POST values will be lost on a redirect, so this snippet will
not 're-enter' the information:
>
<?php
if(isset($_POST['submit'])){
//do your thing here
header('Location: http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
exit;}
>
//displaying of page here
?>
--
Rik Wasmus
| It's also worth noting that this question has been asked so many times
that it shouldn't be too difficult to find some resources on it. | 
August 21st, 2007, 03:05 PM
| | | Re: Question on refresh
..oO(Shelly) Quote:
>Here is a problem I have come across a few times and wonder if there is a
>simple solution.
>
>On my form I have a submit button called, say, addEntry. I do a test for it
>as
>
>if (isset($_POST[addEntry'])) { code to insert stuff into the database }
>
>The problem is that after clicking the button, and the code returns it to
>the same screen, if the user clicks the refresh button on the browser it
>will add another entry. Is there a simple way to unset an html field test?
| After processing the form, send a Location header (with absolute URL)
back to the browser to redirect him to the same page. Now when a user
hits refresh, the browser will just send a normal GET request, not a
POST again. http://www.php.net/header
Micha | | Thread Tools | Search this Thread | | | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 220,989 network members.
|