Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 21st, 2007, 03:15 PM
Shelly
Guest
 
Posts: n/a
Default 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


  #2  
Old August 21st, 2007, 03:25 PM
Rik
Guest
 
Posts: n/a
Default 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
  #3  
Old August 21st, 2007, 03:35 PM
Shelly
Guest
 
Posts: n/a
Default 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


  #4  
Old August 21st, 2007, 03:35 PM
ZeldorBlat
Guest
 
Posts: n/a
Default 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.

  #5  
Old August 21st, 2007, 04:05 PM
Michael Fesser
Guest
 
Posts: n/a
Default 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
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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 network members.
Post your question now . . .
It's fast and it's free

Popular Articles