Perhaps what you really want is to NOT submit/reload the page on each
selection but rather to make all the selections (which may be interdependent
on each other, etc) first, and then click a final "submit" which checks if
all prerequisite fields are filled, and only then submits the form with the
javascript-validated data.
In your case, use an onchange handler directly on the select (
http://www.w3schools.com/TAGS/tag_Select.asp
http://www.w3schools.com/TAGS/ref_eventattributes.asp
http://www.w3schools.com/jsref/jsref_onchange.asp ).
If you need a way to get the javascript value back into velocity, use a
hidden field, e.g:
<input type="hidden" name="jsMessage0" id="jsMessage0"
value="" />
which can be set via this function in JS:
function setJsMessage(slot, str) {
var input_field_jsMessage_o = $('jsMessage'+slot);
if (input_field_jsMessage_o != null) {
input_field_jsMessage_o.value = str;
}
else { alert('error in setJsMessage('+slot+','+str+')'); }
}
After the ititial GET, the user does his thing,. selecting values, etc.
After all the different javascript controls do their thing, and set the
appropriate message slots... the form is finally submitted (POST). In the
POST-handling section of that form (or a different target form) the values
set by setJsMessage(0,....) in javascript can be retrieved in velocity with:
#set( $jsMessage0 = $request.getParameter("jsMessage0") )
It is sometimes confusing when Javascript and velocity are working together.
The issue is that in the initial view of a form (GET), the entire page has
been presented and all velocity variables bound before javascript even runs.
Therefore you need to be careful of the return values of your javascript
onclick() and other callback/handlers: when an onclick handler returns
false, it doesn't invoke form submission (POST). This lets you "precompute"
and validate data in javascript, and then have a final "submit" or
"ok"
(where the onclick handler returns true) which actually POSTs the form iff
all the form data has been determined as valid in javascript.
Of course, this all assumes that the user has javascript enabled...
Niels
http://nielsmayer.com