I have several different submit buttons on my JSP in one form tag that all point to the same servlet. I need to know which submit button was clicked. How can I find out which button was clicked?
if request.getParameter("button-name") is not null then this is the button that was pressed
Each Submit button should have a different name:
<input type="submit" value="This is a submit button" name="submit1">
<input type="submit" value="Another submit button" name="submit2">
<input type="submit" value="Yet another submit button!" name="submit3">
Then, the name of the input should appear in the parameters sent to wherever the form is posting to, something like
post.jsp?key=value&submit3=&....
http://www.w3schools.com/tags/tag_input.asp
This is kind of similar to the DispatchAction in Struts. What they do is to have a hidden field, and when you submit the form, have onClick() set the value to specify which action is taken.
<input type="hidden" name="dispatchAction"/>
<input type="submit" value="Edit" onClick="setDispatchAction('edit')">
<input type="submit" value="Delete" onClick="setDispatchAction('delete')">
<button type="submit" name="somename" value="button1">some text</button>
<button type="submit" name="somename" value="button2">some other text</button>
you will have the post variable "somename" set to the according value, no matter the dispalyed value.
Related
I'm trying to get two values from a jsp page to a servlet, but both are in separate forms:
Drop-down selector:
<form action="${pageContext.request.contextPath}/helloServlet" method="post">
<button class="dropbtn" type="submit" value="test" name="curr_val" id="dropbtn">Choose a Value</button>
</form>
Menu buttons:
<form action="${pageContext.request.contextPath}/helloServlet" method="post">
<button type="submit" name="button" class="demobtn">Menu button 1</button>
<button type="submit" name="button" class="demobtn">Menu button 2</button>
<button type="submit" name="button" class="expbtn">Menu button 3</button>
...
Is it possible to get the value from "Drop-down selector" when I press a "Menu" button? Or do I have to have everything wrapped in one form?
There are a couple of things that might do it for you:
1- If you could put some id's into your forms like form1 with id="form1" and form2 with id="form2", you can do something like:
HTML:
<input type="button" value="Click Me!" onclick="submitForms()" />
JS function:
submitForms = function(){
document.getElementById("form1").submit();
document.getElementById("form2").submit();
}
2- If you could put name into your forms, you could do something like this:
submitForms = function(){
document.forms["form1"].submit();
document.forms["form2"].submit();
}
3- If you cannot modify the HTML and are fine with submitting all the forms on the very JSP, you can do something like:
$('form').submit();
HTML code:
<button id="welcomeContinue" class="btn btn-primary padlock active-removal" type="submit">
<span class="icon-padlock"></span>
CONTINUE
</button>
<input class="btn btn-primary padlock disabled" type="hidden" value="" title="Continue" name="continueanonymousSubmit" alt="Continue">
<input type="hidden" value=" " name="_D:continueanonymousSubmit">
code:
driver.findElement(By.id("welcomeContinue")).click();
How can i select submit button and proceed to next page
You can try using submit() method on a button instead of clicking.
Also, may be some info should be added for submitting to work: some required fields should be filled. Check this option
There are may be some JS scripts running that prevent submit for working
driver.findElement(By.xpath("//button[#type='submit']")).click()
I have a form with 2 submit type buttons(Yes/ No), i would like to handle this form with single #RequestMapping in my controller class. I certainly wish to handle multiple submit in single request mapping method only.
My first question is this possible. Can multiple submit buttons be handled with single request mapping of form action in the controller class ?
If yes, then below is the code I have written. Please suggest if this a correct way of implementing it or if it needs to be updated.
Currently, my code looks like this:
Form.jsp:
<form:form action="doAction">
<input type="submit" name="buttonClick" class="button" value="yes, do Someting" />
<input type="submit" name="buttonClick" class="button" value="no, do nothing" />
</form:form>
Controller.java:-
private String buttonClick;
#RequestMapping(value = "/doAction", method = RequestMethod.POST, params="buttonClick") {
if("yes, do Something".equalsIgnoreCase(buttonClick))
//
else if("no, do Nothing".equalsIgnoreCase(buttonClick))
//
}
You can change the form action on button click e.g. to
"doAction?buttonClick="+<some value from clicked button>.
Or introduce a hidden input in the form. On click change the input value to reflect clicked button. Then the input is available on server side.
You can use a hidden field and change its value on every button click using jQuery:
<form:form action="doAction">
<input type="hidden" name="buttonClick" id="buttonClick" />
<input type="submit" name="buttonClickYes" class="button" value="yes, do Someting" />
<input type="submit" name="buttonClickNo" class="button" value="no, do nothing" />
</form:form>
<javascript>
$(document).ready(function(){
$("input[type=submit]").click(function(){
$("#buttonClick").val($(this).val());
return true;
});
});
</javascript>
I thinked somthing like this, but I dont want to use a button to submit, I would submit by clicking on spring message. Is this possible somehow?
<spring:url value="/admin/messages" var="messagesUrl" htmlEscape="true"/>
<form action="${messagesUrl}" method="POST" class="new-message">
<input type="hidden" name="messageFromDashboard" value="true">
<spring:message code="${newMessage}">
<input type="submit" value="submit"></spring:message>
</form>
Maybe this will be helpful. Why you don't want to use submit button? If you just wanted to look it like a label/link just use css similar to http://jsfiddle.net/adardesign/5vHGc/
Html:
<button> your button that looks like a link</button>
Css:
button {
background:none!important;
border:none;
padding:0!important;
/*optional*/
font-family:arial,sans-serif; /*input has OS specific font-family*/
color:#069;
text-decoration:underline;
cursor:pointer;
}
I have form as given below. Now, when I enter something in the #search_string and press ENTER button it automatically takes it to the action=Paid... I'm not sure why the form gets submitted on the ENTER button..
<!--<div id="startsearchbox">-->
<form id="bigsearchform_new" method="post" action="Paid">
<!--<label style="display:none" for="search_string">SEARCH</label>-->
<input id="search_string" name="search_string" type="text" class="startnewsearch rounded" placeholder="Search..." maxlength="500" >
<input id="searchButton1" type="button" class="searchButton" title="Click here to search the database">
<input type="hidden" name="antiCSRF" value="{{acsrf}}" />
<input type="hidden" name="session_id" value="{{session_id}}" />
<input type="hidden" name="commodity_id" id="commodity_id" />
</form>
It's default behavior of the browser to submit the form on ENTER button. If you want to prevent form submitssion use onSubmit event handler.
That is the default behavior for an HTML form, see http://www.whatwg.org/specs/web-apps/current-work/multipage/forms.html#implicit-submission