Subscribe to get my new tutorials in your inbox.

Reset and clear the HTML form after submitting – when you hit browser’s back button

This is a small issue but sometimes important. Well this is not a major post by me nor a major find, but I did run into some trouble while trying this particular thing out. Hence thought of recording it somewhere, so that I do not have to re-invent the wheel again. Here is the use case – there is an HTML form which is to be filled up and submitted to a server side script (let’s say some PHP code). After successful submission the user hits the back button of the browser. All the form fields and values should be reset and set to its initial configuration.

I tried several different techniques and implementations but ran into some or the other problems mainly because of browsers – different browsers behaving differently. On some browsers something works while on the others it does not. Thanks to these differences in spite of a universal and a common W3C standard. But I finally found a rather easy solution and cached onto it. This post will talk on that particular method with an example.

So the clue is very simple – reset the form after it is submitted so that when the user hits the back button every field in the form is initialized and reset (no previous values are left behind). How?? Let’s see in this example,

First I have a simple HTML form with a checkbox, two input fields and a button,

  <form method="get" name="myForm" action="submit.php">
    Salaried: <input type="checkbox" name="salaried"/>
    First Name: <input type="text" name="input1"/>
    Last Name: <input type="text" name="input2"/>
    <input type="button" value="Submit" onclick="submitForm()"/>
  <script type="text/javascript" src="form_submit_script.js"></script>

Note that the button is not a submit button, but a normal push button. I have a onclick event handler attribute assigned to it which calls a function submitForm() when user clicks on it. Inside this function I do a small validation and then finally submit the form. The trick to remove the previous values when you hit the back button in the browser is inside this function. Let’s check it out,

function testSubmit()
  var x = document.forms["myForm"]["input1"];
  var y = document.forms["myForm"]["input2"];
  if(x.value =="" || y.value=="")
    alert('Not allowed!!');
    return false;
  return true;
function submitForm()
    document.forms["myForm"].submit(); //first submit
    document.forms["myForm"].reset(); //and then reset the form values

You can see the submitForm(){} function definition in the code block above. First I do a form validation for empty fields, which is very simple and I will not go inside it. After successful validation the form is submitted using the standard submit() method and then I reset the form using the standard form reset() method. This ensures that every form field is reset and there are no values left behind in the text boxes. You van verify this by first checking the checkbox, putting some values in the text boxes and clicking on the button. This will take you tot he submit php page (The PHP page is not in my server, so you get a 404 error). Then click the back button of your browser and see that the checkbox and text inputs are reset. Try the same with the last line in the script commented as shown below,


You will notice previous values being left behind. So, a very simple way to tackle this, but an effective one.

And this way of resetting works on all major browsers.

      • Erna

        I discovered that even though the reset code works to reset the form it has the unintended result of preventing the field required messages to work. Normally a user will be prompted to complete empty compulsory fields before submitting the form, but after adding the reset function, you simply get a cgi error message if there are any empty fields, i.e. it skips the step of prompting you to complete the form.

  1. nmczyber

    Well, it works fine, but I think that it’s much better solution is a creating a function like this

    function FormReset(){

    And just…
    Or course in form we need to add id=”ClearIt” for each element…
    Sorry because my bad English…

  2. Allon de Veen

    Actually, I don’t know if you code PHP yourself, but if you want to clear the form, you should really use php to clear the POST array. Like so: $_POST = array(); . That is only possible if you code the php that submits the form yourself. Otherwise you should really use the other method.

  3. Hitarth Choubisa

    I have a similar problem regarding resetting the values. Actually, I have a text file on my server and I want that the input data fetched from the user may stay there for sometime, say 10 seconds and after that it has to be reverted back to the original value. I am unable to get any idea of how to achieve it. Could you please help! Thanx in advance.

      • Hitarth Choubisa

        Hi Joseph,

        I actually want to control a remote camera on my phone to capture photos according to the desire of the user. So, to pass on the instructions from user on my webpage to android I need to store the command for capturing photo for sometime, say 10 seconds in form of a bit (1) on a file on my web server. I want this 1 to revert back to (0) automatically at the end of 10 seconds so that the phone may not get continous instruction of capturing the photos. So, problem is regarding this automatic reverting back. Please help me & Thanx for showing interest.


  4. moonamoona

    Hi joseph,
    I am a newbie please help me I have some problems. I have 2 forms, what I want is when user submit the first form he will be redirected to the 2nd form and will not be able to come back to the first form and see the data that he/she inserted. I have form1.php with form action=process.php. where process.php file is where I insert user answers into database and I used header(Location:form2.php) at the end of this file to redirect user to the 2nd form after submitting the first one. I dont know where I have to write your submitForm function.. also I added onclick=… and changed input type to “button” instead of “submit” but nothing happened. plase help me.. I really need it ..

    • joseph

      I would suggest you to go with the Single Page Application architecture. So, when form 1 is submitted replace its HTML with form 2. This way the user would not be able to come back to form 1.

  5. David Prout

    I got around this a different way, being:


    $(window).on(“unload”, function () {
    document.getElementById(“form ID”).reset();

    Pure Javascript:

    window.onunload = function() { document.getElementById(“form ID”).reset(); }

  6. kas

    Can someone help me put in code this word 2013 form for clearing/resetting the form for the next user? It will submit the form via email with a pop up confirmation message but, I can’t figure out the clear/reset code or where it needs to be placed here:

    ‘No-option email sending
    Private Sub CommandButton1_Click()
    Dim OL As Object
    Dim EmailItem As Object
    Dim Doc As Document

    Application.ScreenUpdating = False
    Set OL = CreateObject(“Outlook.Application”)
    Set EmailItem = OL.CreateItem(olMailItem)
    Set Doc = ActiveDocument

    With EmailItem
    .Subject = “Reimbursement Form”
    .Body = “Submission of Reimbursement Form” & vbCrLf & _
    ” ” & vbCrLf & _
    ” ”
    .To = “[email protected]
    .Importance = olImportanceNormal
    .Attachments.Add Doc.FullName

    End With

    Application.ScreenUpdating = True
    MsgBox “Your form has been submitted for processing. Thank you!”

    Set Doc = Nothing
    Set OL = Nothing
    Set EmailItem = Nothing

    End Sub

Write a Comment

Your email address will not be published. Required fields are marked *