Javascript - Forcing a form submit


Sometimes it's desirable to force a form to be submitted without bothering the user to click on the Submit button.  In the example below the form contains <select> group with several <options>'s.  When any option is clicked the form is submitted. 

code:

<form name='get_date_form' method='GET' action='calendar.php'>
	<select name='game_date' size='1' onChange='document.get_date_form.submit();'>
		<option  >-- Select Game Date --</option>
		<option  >July 5, 2007</option>
		<option  >July 12, 2007</option>
		<option  >July 19, 2007</option>
		<option  >July 26, 2007</option>
	</select>
</form>

The important point to notice is that in order to do this the form must be named - in this case it's called "get_date_form".   When the selection is changed, the onChange event is triggered causing the javascript applet to be executed, which, in turn, does a form submit.

The one thing you need to plan for is what if the item currently selected is the one you want to process.  There are two ways around this.  One trick is to make a top level option be the default (in this case the one that reads "--Select Game Date--").  Then the selection will always change when any real option is picked.

The other method, the one used in the bUGS attendance page, is to assume that the first item in the list is the one most likely to be selected and go ahead and display that page.  The only if a different date is required wil the user have to do anything.