How to Prevent the "Confirm Form Resubmission" Dialog

Preventing form resubmission

There are 2 approaches people used to take here:

Method 1: Use AJAX + Redirect

This way you post your form in the background using JQuery or something similar to Page2, while the user still sees page1 displayed. Upon successful posting, you redirect the browser to Page2.

Method 2: Post + Redirect to self

This is a common technique on forums. Form on Page1 posts the data to Page2, Page2 processes the data and does what needs to be done, and then it does a HTTP redirect on itself. This way the last "action" the browser remembers is a simple GET on page2, so the form is not being resubmitted upon F5.

How to prevent confirm form resubmission popup after failed form submission

Trick is to keep form data in a session and populate form type with the data stored in same session for failing forms submissions. Data stays in session until successful form submission.

Full example is here: Preventing 'Confirm form resubmission' dialog in symfony applications

/**
* @Route("/interest", service="my_test_bundle.controller.interest")
*/
class InterestController
{
private $formFactory;
private $router;

public function __construct(
FormFactoryInterface $formFactory,
RouterInterface $router
) {
$this->formFactory = $formFactory;
$this->router = $router;
}

/**
* @Method({"GET"})
* @Route("", name="interest_index")
* @Template()
*
* @param Request $request
*
* @return array
*/
public function indexAction(Request $request)
{
$form = $this->createForm();

$session = $request->getSession();
if ($session->has($this->getFormName())) {
$request->request->set(
$this->getFormName(),
unserialize($session->get($this->getFormName()))
);
$request->setMethod('POST');
$form->handleRequest($request);
}

return ['interest_form' => $form->createView()];
}

/**
* @Method({"POST"})
* @Route("", name="interest_create")
*
* @param Request $request
*
* @return RedirectResponse
*/
public function createAction(Request $request)
{
$session = $request->getSession();

$form = $this->createForm();
$form->handleRequest($request);
if ($form->isValid()) {
// Do something with: $form->getData();

$session->remove($this->getFormName());
$routeName = 'home_or_success_route';
} else {
$session->set(
$this->getFormName(),
serialize($request->request->get($this->getFormName()))
);

$routeName = 'interest_index';
}

return new RedirectResponse($this->router->generate($routeName));
}

private function createForm()
{
return $this->formFactory->create(
InterestType::class,
new Interest(),
[
'method' => 'POST',
'action' => $this->router->generate('interest_create'),
'attr' => ['id' => $this->getFormName()],
]
);
}

private function getFormName()
{
return InterestType::NAME;
}
}

How can I disable confirm form resubmission messages?

After processing POST on your page

example.com/mypage.php

use

header('Location: example.com/mypage.php');

In this way the post will not be resubmitted and you wont get the alert from browser.

Allowing users to Refresh browser without the Confirm Form Resubmission pop-up

After processing the POST page, redirect the user to the same page.

On http://test.com/test.php

header('Location: http://test.com/test.php');

This will get rid of the box, as refreshing the page will not resubmit the data.

Confirm Form ReSubmission in django

I suggest you read this, which is very close to your question. In conclusion, you need to use a return HttpResponseRedirect, even if you are only redirecting to the same view.

More related question from Stack Overflow please click here.



Related Topics



Leave a reply



Submit