Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MythicFrost

macrumors 68040
Original poster
Mar 11, 2009
3,940
38
Australia
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 i = 0; i < controlNames.length; i++) {
        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.
 

Darth.Titan

macrumors 68030
Oct 31, 2007
2,905
753
Austin, TX
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.
 

MythicFrost

macrumors 68040
Original poster
Mar 11, 2009
3,940
38
Australia
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.
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
 

Darth.Titan

macrumors 68030
Oct 31, 2007
2,905
753
Austin, TX
Yeah some browsers need the onload, some need the onunload. For best cross compatibility, you gotta use both.

Glad I could help.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.