tags:

views:

120

answers:

3

hi all

i am resetting my form with the following:

<script type="text/javascript">

window.onload = function () {
document.getElementsByName("email_address")[0].value = "";
document.getElementsByName("remove")[0].value = "off";
}



</script>

the problem is that the checkbox "remove" seems to get reset before the server side script can run. So in effect what ever the user selected on the checkbox is overridden and set to off before the php script can be run. Yet, the input box "email_address" is fine...

the test...

//this is already set to OFF by the time we get here....even if the user may have ticked the checkbox to ON
echo $HTTP_GET_VARS['remove'];

//this is fine, the email_address input is read and not reset...
echo $HTTP_GET_VARS['email_address'];

can any one explain this? I obviously want the form to be reset AFTER my server side script is done processing , yet for some reason it resets the checkbox early, yet the input box is ok...

thank you!

+1  A: 

try with the following instead of off

document.getElementsByName("remove")[0].checked = false;

cheers

PERR0_HUNTER
thanks for the tip but unfortunately this didnt work :(
chicane
i tried all these tips today again and this one proved to work for me now, thank you. i think i was just too tired yesterday to implement the solution properly :o
chicane
+3  A: 

I'm really confused. You say you don't want to have the form cleared before it returns a post-submit value (are you doing an ajax form here?) yet the sample code you posted is set to execute when the page loads. Of course it's going to reset before a submit takes place.

Instead, you need to set it up to execute when the form submission is complete and you have the response you expect.

And don't bother with manually resetting each form element, just use the form.reset() method instead.

Peter Bailey
Hi thanks for the tip, can you tell me how I would go about executing the javascript reset after the form submission is complete? thanks
chicane
Sure, but first you need to share how it is you're submitting the form. Modify your original question to include some example code of how you trigger the form submission.
Peter Bailey
+1  A: 

Why not just...

document.forms[0].reset();
Josh Stodola