Getting values in servlet from two different forms - java

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();

Related

It is possible to submit a hidden input by clicking on <spring:message>?

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;
}

Form gets submitted on ENTER button

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

Wicket: Two AjaxButtons inside separate forms issue

I want to place two separate file upload panels on page, one is for images, another for other files. Each panel contains a form with AjaxButton inside. First I developed the first panel, it worked as expected. But when I added another one, the second AjaxButton inside a form is not responding when clicked. Instead, when I click on the first button, the second one responds. What could be the reasons for that?
Here's the first panel's HTML:
<wicket:panel>
<div wicket:id="form-container-img">
<form wicket:id="form-img">
<img wicket:id="releaseImage" class="thumbnail" />
<p>
<label>Select file :</label>
<input wicket:id="fileUpload-img" size="40" type="file"/>
<input wicket:id="uploadButton-img" id="uploadButton" type="submit" value="Upload" />
</p>
</form>
</div>
</wicket:panel>
And the second one:
<wicket:panel>
<div wicket:id="form-container-album">
<form wicket:id="form-album">
<span wicket:id="releaseName" />
<p>
<label>Select file :</label>
<input wicket:id="fileUpload-album" size="40" type="file" />
<input wicket:id="uploadButton-album" id="uploadButton" type="submit" value="Upload" />
</p>
</form>
</div>
</wicket:panel>
So now, when both panels are in place, clicking uploadButton-album does nothing but it responds by clicking uploadButton-img.
EDIT:
<wicket:panel>
<div wicket:id="uploadPanelReleaseImg"></div>
<div wicket:id="uploadPanelReleaseAlbum"></div>
</wicket:panel>
In html markup, the id attribute should always be unique on the page. I'm not sure it's required by any spec, but it's generally regarded as good practice, and things tend frequently to depend on it.
If you need to control the id (because you're referencing it in JavaScript and it's easier than figuring out what id Wicket generated), you need to manage this manually.
If you simply omit the id attribute, Wicket will generate one based on the wicket id, and guarantee uniqueness on the page.
Based on the comment thread, this appears to have been the problem.

navigating through tab key not working properly

in my jsp, when i navigate through tab key it skips my Save and Reset button. and move to next component in my page. even though i have not used tabindex in my jsp files. please suggest what could be the reason. thanks
code is like:
<div>
<input type="hidden" name="_flowExecutionKey" value="${flowExecutionKey}"/>
<input type="submit" class="button" name="_eventId_search" value="Search"/>
<span class="save_button_common" onClick="submitForm('save')">Save</span>
<span class="reset_button_common" onClick="submitForm('reset')">Reset</span>
</div>
May be because it is not input type , you can explicitly try setting tabIndex
Try:
<div>
<input type="hidden" name="_flowExecutionKey" value="${flowExecutionKey}"/>
<input type="submit" class="button" name="_eventId_search" value="Search"/>
<input type="button" class="save_button_common" onClick="submitForm('save')">Save</input>
<input type="button" class="reset_button_common" onClick="submitForm('reset')">Reset></input>
</div>
Note if it's a form you need input type="button" rather than just using <button> which you can otherwise just for straight links that don't submit a form. This is particularly relevant with Internet Explorer that treats the button tag somewhat differently to other browsers (submitting the "value=" rather than the enclosed text or something like that)

How can I tell which submit button was clicked

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.

Categories