FORMS: disable re-sending the form contents on page refresh

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
koguee
Forum Newbie
Posts: 18
Joined: Thu Aug 14, 2008 5:13 am
Location: Philippines
Contact:

FORMS: disable re-sending the form contents on page refresh

Post by koguee »

sure you have once experienced this so
anyone who has an insight on how to do this?

thanks.
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: FORMS: disable re-sending the form contents on page refresh

Post by andyhoneycutt »

How about some code, or some pseudo-code, or some sort of explanation? :banghead:
paperplate
Forum Newbie
Posts: 16
Joined: Thu Sep 04, 2008 12:15 pm

Re: FORMS: disable re-sending the form contents on page refresh

Post by paperplate »

koguee wrote:sure you have once experienced this so
anyone who has an insight on how to do this?

thanks.
I think I know what you mean. User fills out a form, submits it and then refreshes the page...this is a problem only when your form method is "post" though. So the easiest fix is just to use "get" if it's practical. If you stay with "post" you can just redirect them to some other page that isn't a form handler. Example: they fill out form on form.php, method=post,action=form.php. They submit form, and you record the input. Then you display a "entry submitted, click here to continue" or just redirect them to some other page.

Like andyhoneycutt mentioned, some examples of what you're trying to do will greatly help us & you so that we all can determine a feasible solution.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: FORMS: disable re-sending the form contents on page refresh

Post by Christopher »

The standard practice is to redirect once the form is accepted. Here is example pseudo-code:

Code: Select all

if ($request->isPost() ) {
     if ($form->isValid($request) ) {
          $model->save($form->getData() );
          header('Location: http://mysite.com/form/done/');
          exit;
     }
} else {
    $form->initialize($model->getData() );
}
echo $view->render($form);
(#10850)
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: FORMS: disable re-sending the form contents on page refresh

Post by andyhoneycutt »

i completely agree with arborint. If the goal here is to eliminate the possibility of re-submission, redirect the end-user to a page that contains no form or values, or at least none that can modify the previous input. This is a safe route, and more-or-less what I tend to do on my sites as well.

-Andy
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: FORMS: disable re-sending the form contents on page refresh

Post by jayshields »

An alternative option could be to dump the whole of $_POST into a session variable, for example, $_SESSION['previous_post'] and then the next time a $_POST request is submitted, check to make sure it's not identical to the one stored.

I've just come up with that now, so it probably isn't bullet-proof.
Post Reply