Forum Moderators: open

Message Too Old, No Replies

reset select box default on back button event

         

rcjordan

4:54 am on May 5, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have one select box in a form that I'd like to be able to reset to the selected option when the user returns to the form via the back button in order to make corrections. Use the onsubmit event, perhaps?

txbakers

5:07 am on May 5, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What server side tech are you using? I do this with ASP.

The user fills out the form and hits submit. A server side script validates the form, and if not valid (duplicate ID for example), the redirect is set to the same page, and all the form fields, including the select and checkboxes are set to what the user entered using value = Response.Form("field")

Works very nicely - especially on one page where there are about 25 or more user inputs. It was very annoying to the user to go back to a blank page after everything was entered.

joshie76

11:39 am on May 5, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This stuff is tricky - we recently wanted to do some similar stuff with forms that led to some experimentation with the way browsers deal with forms and history.

It seems that when a page is unloaded (submit or whatever) the history not only remembers the page it remembers all the values of all form elements at the point of exit (including those set by JavaScript - not just user modifications). Also you have no server scripting power as clicking back doesn't contact the server for the last page loaded - it takes it all from memory.

You should be able to use an onload event to select te correct selectedIndex. eg:

<body onload="document.myForm.mySelectBox.selectedIndex=1">

When the page is loaded normally the selectedIndex will be selected as you wished. When the user clicks back the onload event will still fire selecting your chosen select option - overriding that in the browsers history.

I have to admit I haven't yet tried this in NN4 or Opera - let us know how you get on.

rcjordan

4:13 pm on May 5, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<body onload="document.myForm.mySelectBox.selectedIndex=1">

>This stuff is tricky

Yes, but the above works just fine. For others that might use this, I'll add that the selectedIndex= apparently counts the number of options starting with zero, so the above example actually selects the 2nd option in the selectbox.

>NN4

I tried it on N4.08, N6.2, & IE 6.0.2600 (PC). Works. Thanks!