Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent form resubmit after pressing back button

I am in bit of a delicate situation here. In my organization we design stock management systems and it is a web application based on JSP pages and servlets which handles them.

I have been asked to fix a specific problem. We have a JSP page with an HTML form table where there are stock details. When user enters the details manually and submit the form, stock details updated in the database and it works fine.

Problem is this : When the user press the browser's back button, user can come to the previous page where he submitted the details. And when the user submit this, data is saved once more to the database.I need to prevent this behaviour.(Something likeclear and reload the page.)

Things I did so far : clear the browser cache.Code works fine but not the expected result.

Unfortunately I cannot share the code due to company regulations. What I need is a help to prevent this behaviour or a workaround.

Thanks in advance..

like image 246
Lucy Ferrier Avatar asked Sep 12 '17 18:09

Lucy Ferrier


People also ask

How do you stop re submitting a form after clicking back button?

You can check if the user clicked the back button, disable form if true. Another way is by storing a cookie which you check on page load, if it exists you can disable the form.

How do I restrict form resubmission?

You can prevent form resubmission via a session variable. Yes we can use microtime() as well as time() also instead of rand() , whatever function or variable that gives different value we can use it. BUT make sure that you set that value to SESSION variable.

How do I stop confirmation resubmission?

Solution 1: Disable Confirm Form Resubmission From ChromeRight click on your chorme shortcut, select properties. In the target field, add: “-disable-prompt-on-repost” without the quotes after chrome.exe.

How do I stop resubmit from refreshing in asp net?

As you probably know, ASP.NET Web Forms send POST requests back to the server and then re-render the Page in the same request. This is why we see the form re-submission message when we click "reload". To avoid this issue, we should employ the post-then-redirect pattern used by many web applications.


1 Answers

You can use a javascript function with the help of a hidden attribute to reload the web page. When the user press the back button,based on the value of the hidden attribute, page will be reloaded without loading the cached page.

Your approach of clearing cache is correct. Coupled with that, you can use this approach.

<input type="hidden" id="refreshed" value="no">
    <script type="text/javascript">

           onload=function(){
               var e=document.getElementById("refreshed");
               if(e.value=="no")e.value="yes";
               else{e.value="no";location.reload();}
           }

    </script>

One drawback of this approach is if your clients' browsers have disabled JS, this will not work.Otherwise it should work.

like image 91
Supun Amarasinghe Avatar answered Sep 23 '22 15:09

Supun Amarasinghe