Page 1 of 1

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

Posted: Thu Sep 04, 2008 8:45 pm
by koguee
sure you have once experienced this so
anyone who has an insight on how to do this?

thanks.

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

Posted: Thu Sep 04, 2008 8:51 pm
by andyhoneycutt
How about some code, or some pseudo-code, or some sort of explanation? :banghead:

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

Posted: Thu Sep 04, 2008 9:19 pm
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.

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

Posted: Thu Sep 04, 2008 10:14 pm
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);

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

Posted: Fri Sep 05, 2008 12:51 am
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

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

Posted: Fri Sep 05, 2008 8:23 am
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.