Clear text boxes, etc., if you hit back on the browser?

Discussion in 'Web Design and Development' started by MythicFrost, Oct 18, 2011.

  1. MythicFrost macrumors 68040

    MythicFrost

    Joined:
    Mar 11, 2009
    Location:
    Australia
    #1
    Hey,

    I've got several text fields and such on my form, and when you hit submit it goes to the next page which says "Thanks for signing up", more or less. However, if you hit the back button it goes back to the webpage and all the previously entered details are there.

    How would one go about clearing them? I thought I knew how but it's not been working. So far I've written a javascript function:
    PHP:
    <script type="text/javascript">
    function 
    clearForms() {
        var 
    controlNames = ["Test1""Test2""Test3""Test4""Test5"];
       
        for (var 
    0controlNames.lengthi++) {
            var 
    ele document.getElementById(controlNames[i]);
            
    ele.value "";
        }
    }
    </script>
    And this code runs... (I've confirmed this with an alert()) however when I hit back it has all those details there. The function clearForms() is called on onLoad, and I've also tried on unLoad, as well as onSubmit and onReset on the actual form (rather than the entire page). However, it's not worked so far.

    Any thoughts on what I might be doing wrong? Thanks.
     
  2. Darth.Titan macrumors 68030

    Darth.Titan

    Joined:
    Oct 31, 2007
    Location:
    Austin, TX
    #2
    My first thought is, why? If a user backs up to a registration page even after successfully submitting a form, why does it matter that their input is retained? Why would a user do that to begin with? Surely if they were to submit again your system would recognize a duplicate entry, correct? In my opinion you're over-thinking it.

    My second thought is that if you really want the form fields cleared, they'll need to be cleared every time the page is loaded.
    Code:
    <body onload="document.forms[0].reset();" onunload="document.forms[0].reset();">
    That might mess with any server-side validation you might be using though.
     
  3. MythicFrost thread starter macrumors 68040

    MythicFrost

    Joined:
    Mar 11, 2009
    Location:
    Australia
    #3
    It was requested by the client. There's no server-side stuff, it's just to send an e-mail with details from the text boxes. Do I need the .reset for unload for any reason? Thanks, it works.

    EDIT: It seems I do need the unload part... thanks again
     
  4. Darth.Titan macrumors 68030

    Darth.Titan

    Joined:
    Oct 31, 2007
    Location:
    Austin, TX
    #4
    Yeah some browsers need the onload, some need the onunload. For best cross compatibility, you gotta use both.

    Glad I could help.
     
  5. MythicFrost thread starter macrumors 68040

    MythicFrost

    Joined:
    Mar 11, 2009
    Location:
    Australia
    #5
    Ah right, I see. Thanks a lot!
     

Share This Page