I am creating a page in JSP where I have a dropdown list and once the user selects a value he has to click on the go button and then the value is sent to the Servlet.
</select>
<input type="submit" name="GO" value="Go"/>
How do I make it so that it does it on change? E.g. when the user selects John all his details are retrived from the DB and displayed. I want the system to do it without having to click the go button.
Just ask assistance of JavaScript.
<select onchange="this.form.submit()">
...
</select>
See also:
HTML dog - JavaScript tutorial
Simple JavaScript will do -
<form action="myservlet.do" method="POST">
<select name="myselect" id="myselect" onchange="this.form.submit()">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
<option value="4">Four</option>
</select>
</form>
Here is a link for a good javascript tutorial.
other than using this.form.submit()
you also submiting by id or name. example i have form like this : <form action="" name="PostName" id="IdName">
By Name :
To those in the answer above. It's definitely JavaScript. It's just inline.
BTW the jQuery equivalent if you want to apply to all selects:
$('form select').on('change', function(){
$(this).closest('form').submit();
});
Success story sharing
<noscript>
banner that site's experience is better if JS is enabled. A completely different alternative is to use<ul><li><input type="submit">
and throw in a load of CSS to make it look like a true dropdown list. But then there's no means of a<select>
anymore.<noscript>This form requires that you have javascript enabled to work properly please enable javascript in your browser.</noscript>
also @NathanHornby if you really want a citation why dont you go to a public library most public library's block javascript for security reasons as well as some schools too so don't try to pass it off as a myth it has and is being done mainly for security reasons to keep the integrity of computers as some javascript is malicious code...submit
button in<noscript>
tags. It will only show if the users use noscript. Users who have JS enabled can simple select the element, and users with noscript will just click the submit button. :)