1

Let say I have a HTML form and a submit button. When I click the submit button I submit a post request to the server for processing.

How do I prevent a refresh of the page while the request is been processed by the server. Please note that the server has not returned any response yet and the spinner is still spinning in the browser. This is the time where I want to disable refresh as the current request is still in process.

I have implemented POST/REDIRECT/GET , but the issue is that even before the server redirects , and if the request is still in process , then I want to disable the refresh till the server redirects the request.

Chetan
  • 4,735
  • 8
  • 48
  • 57
  • Maybe you find here a solution: https://stackoverflow.com/questions/5711815/can-i-disable-browser-refresh-in-my-webapp – Mike Jun 13 '19 at 07:17
  • Afaik there's nothing you can do to prevent this user action 100%. The browser should display a dialog box asking the user if they really want to submit the form data again, and if they confirm, the browser will send the entire POST request a second time. The only way to prevent this is passively, on the backend. –  Jun 13 '19 at 07:26
  • 1
    Why not an ajax request with javascript redirect? – nice_dev Jun 13 '19 at 07:38

1 Answers1

0

you need to event.preventDefault() to prevent refresh of page

<form action="#" method="post" onsubmit="return ValidationEvent()">
...
...
<script>
function ValidationEvent(event) {
event.preventDefault(); //prevent refresh of page

// your ajax api call here
}
</script>
Arshpreet Wadehra
  • 993
  • 2
  • 9
  • 23