Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

prevent information from being sent again on refresh?

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

prevent information from being sent again on refresh?

Postby tranquillo » Tue Jul 08, 2003 2:59 am

Hi...

I was just wondering if anyone know if there's any way around the fact that information in a form wil be posted twice if someone uses the refresh button?

thanks.
tranquillo
New php-forum User
New php-forum User
 
Posts: 74
Joined: Sun Nov 24, 2002 6:48 pm
Location: katrineholm

Postby Oleg Butuzov » Tue Jul 08, 2003 3:38 am

keywords
java script onclick disable
Oleg Butuzov
Last Samuray
Last Samuray
 
Posts: 831
Joined: Sun Jun 02, 2002 3:09 am

Postby tranquillo » Tue Jul 08, 2003 7:10 am

ok... don't think that's what I'm looking for, don't want to disable the refresh function, I guess that's what you ment...
tranquillo
New php-forum User
New php-forum User
 
Posts: 74
Joined: Sun Nov 24, 2002 6:48 pm
Location: katrineholm

Postby Joan Garnet » Tue Jul 08, 2003 8:48 am

Maybe if you send those vars to another page saying "sending data" or whatever and then redirecting to the original page again.
Just like in this forums :D
User avatar
Joan Garnet
Moderator
Moderator
 
Posts: 387
Joined: Sat Aug 03, 2002 2:56 am
Location: Mars

Postby swirlee » Tue Jul 08, 2003 10:38 am

What Pejone means is that you should include some Javascript on your page that will disable the Submit button as soon as a user clicks on it. This is very useful for when the user's connection to your server is slow and it takes a long time for the form to be submitted. Often users get impatient and will click Submit a second time if the next page doesn't load, even though the data has perhaps already been submitted, causing a double-post or whathaveyou.

What Joan means is that you should have your form submit to an intermediate script. For example, if your form is in form.php and you want the user to end up at result.php after submitting the form, have the form submit the data to another script called process_form.php. The file process_form.php will contain no HTML and echo no text to the user. All it will do is process the information and then forward the user to result.php, like so:

Code: Select all
<?
   /*
      Your database calls or whatever will go here
   */

   header("Location: result.php");
?>


The header() command will immediately forward the user to result.php, and a user pressing Reload on result.php won't cause any har. If you want to pass along any of the data that was submitted to the form, you can do so via the query string like so:

Code: Select all
<?
// blah blah blah

header('Location: result.php?name=' . $_REQUEST['name'] . '&city=' . $_REQUEST['city']);
?>


You could also potentially do this with sessions, or if you happen to be inserting these into a database, just pass along the row id. I hope that's helpful.

Also, there's a discussion of the double-post problem on this page that might interest you, and the writer proposes a particularly novel and viable solution.
User avatar
swirlee
Moderator
Moderator
 
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back


Return to PHP coding => General

Who is online

Users browsing this forum: Google [Bot] and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron