postback to server executing different queries
I have a few <div>
inside a forms, each thing inside the div
contains a specific form.
When a user presses the submit button, I want to execute a different actio开发者_如何学JAVAn based on
<form method="get" action="addprogramtodb.jsp">
<select name="cid" style="display: none;">
<option>1</option>
<option>2</option>
</select>
<div id="1">
</div>
<div id="2">
</div>
<div id="3">
</div>
<input type="submit"/>
</form>
When the user presses the submit button I want the program to execute different queries based on what div it is in.... based on the div id, or somehow..
Give the submit button a name and value the usual way.
<input type="submit" name="action" value="action1">
...
<input type="submit" name="action" value="action2">
...
<input type="submit" name="action" value="action3">
The pressed button is namely available as request parameter as well.
String action = request.getParameter("action");
if ("action1".equals(action)) {
// action1 button is pressed.
} else ("action2".equals(action)) {
// action2 button is pressed.
} else ("action3".equals(action)) {
// action3 button is pressed.
}
You can if necessary give them a different name instead and then nullcheck each request parameter.
<input type="submit" name="action1" value="This is more i18n friendly">
...
<input type="submit" name="action2" value="Blah">
...
<input type="submit" name="action3" value="More blah">
with
if (request.getParameter("action1") != null) {
// action1 button is pressed.
} else (request.getParameter("action2") != null) {
// action2 button is pressed.
} else (request.getParameter("action3") != null) {
// action3 button is pressed.
}
Or, if they are actually all in their own <form>
, then you can also pass a hidden input along.
<form>
<input type="hidden" name="action" value="action1">
...
</form>
<form>
<input type="hidden" name="action" value="action2">
...
</form>
<form>
<input type="hidden" name="action" value="action3">
...
</form>
with the same server-side handling as in 1st example.
精彩评论