How to detect which submit button was clicked when the names are unknown? - forms

I know how to detect which submit button was clicked when I know the name values of each of the buttons. But what if the names are dynamic or defined by another component?
For example, here I can simply check the POST data from this <form> for either alpha or bravo:
<form>
<input type="submit" name="alpha" value="Alpha">
<input type="submit" name="bravo" value="Bravo">
</form>
But that's only because I know I should be looking for those names.
Is there a best practice for handling this type of situation? (Perhaps by rendering an element <input type="hidden" name="submit-button-names" value="dynamic_name1|dynamic_name2|etc">.) I would like a solution that doesn't require JavaScript.

Presuming you have control over the JSP displaying these buttons, just prefix the button names with a string you can look for in the POSTed data. For example prepending "dynamicbutton_" to all of the names like this
<form>
<input type="submit" name="dynamicbutton_alpha" value="Alpha">
<input type="submit" name="dynamicbutton_bravo" value="Bravo">
</form>
Then in your Servlet, look for values with this prefix by calling ServletRequest.getAttributeNames()

You could write a custom tag to set the different inputs to your form based on a list of parameters you give to the tag.
You would end up with the HTML looking something like this:
<form method="POST" action="SelectColour.do">
<p>Select your favorite colour: </p>
<formTags:select name='colour' size='1' optionsList='${applicationScope.colourList}'/>
<input type="SUBMIT" value="Click here to submit">
</form>
Here's a decent guide to creating custom tags.

Related

Vuejs how to prevent form submit on enter but still save work in inputs?

This prevent enter submit,but at the moment prevent enter in inputs.
I need that enterkey work in inputs but not submit form.
<template id="form-template">
<form action="save-passport" #keypress.enter.prevent method="post" enctype="application/x-www-form-urlencoded" >
<input type="submit" v-show="loaded" v-on:click="saveForm" value="Save" />
<input type="text" required="required" name="field-11" >
<select required="required" name="field-13"><option value=""></option>
</select>
</form>
</template>
Had the same problem. Solution I did was to put the submit method on the opening form tag. Then no need for the input/button type="submit" inside the form at the bottom.
<form class="form" action="" method="POST" #submit.enter.prevent="createForm()">
Also other options available on inputs:
#keydown.enter.prevent
#keyup.enter.prevent
#submit.enter.prevent
I did it this way
first i removed vue modifiers from form tag.
second i chaged type of input from submit to button
and third this my saveForm function
saveForm:function(){
if (someCondition){
event.preventDefault();
}else{
document.forms[0].submit();
}
}
You need to use the prevent modifier. Another thing, your webpage itself must have the focus. Meaning, if you just visit the webpage, prevent won't work. But, if you click somewhere in the form, prevent will work for you.
You can still submit the form via JavaScript using something like document.forms[0].submit();

Is there a way to label the Go button in iPhone Safari?

When focus is placed into an input of this form, iPhone shows input keyboard and Go button, which acts as submit. Is it possible to change the label to e.g. "Create"? I tried value, title or name but none of those work.
<form>
<input name="foo" type="text"/>
<input type="submit" name="Create" value="Create" title="Create"/>
</form>
No, something like this is not possible.

input buttons and 508 compliance: do they really need labels?

Does a button with a value really need a matching label tag in order to be 508 compliant?
Say I have:
<label for="search_box" class="hideme">search query</label>
<input type="text" id="search_box">
<input type="button" id="search" value="Search">
The web accessibility testing software is flagging it since it has an id but no matching label? Is it really necessary?
A <label> is not needed for <input type="button">, you need to provide a value attribute. If the button is using an image versus text, you need to provide an alt to be compliant.
<input type="button" src="....jpg" alt="Search" id="...">

Binding multiple text boxes with multiple submitt buttons in same form

I have a form where I have a search functionality with a text box(TextBox1) and a submitt button(Button1). Apart from search, there is another set of textbox(TextBox2) and submitt button(Button2). When I write something in search box(TextBox1), and hit enter, the validation message of the second textbox(TextBox2) is shown.
I am not sure how to bind the respective textboxes with submitt buttons. Please help.
Thanks in advance
Depending on how much control you have, the easiest way would be to have separate forms for each texbox/button combo.
<form action="dosomething.php">
<input type="text" name="foo" />
<input type="submit" />
</form>
<form action="dosomethingelse.php">
<input type="text" name="bar" />
<input type="submit" />
</form>
If you can't or don't want to do that, you'll need to look at handling the onkeydown event on the text boxes to prevent an automatic submission. Something like <input type="Text" name="foo" onkeydown="doSubmit(this); return false;" />. Note the return false;, which prevents the default action from taking place.

Using anchor link for form submission

Can i use an anchor tag to submit a form? In this code i am using a submit button which is standard but how to use a anchor tag <a href=Cart?removeId=${cartItem.productId>Remove</a>
I tried this, but the doGet() method is being called in the servlet. I want to call the doPost() method obviously. So could i use a better approach.
<c:forEach items="${lstCart}" var="cartItem" varStatus="count">
<form action="Cart" method=Post>
<tr height="40px">
<td>${count.count}</td>
<td>${cartItem.productName}</td>
<td>${cartItem.quantity}</td>
<td>${cartItem.unitPrice}</td>
<td>${cartItem.totalPrice}</td>
<td>
<input type="hidden" name="id" value="${cartItem.productId}" />
<input type=submit value="x"></td>
</tr>
</form>
</c:forEach>
You need to use JavaScript to make an anchor link do a post. You might use CSS to style your submit button and make it look more like a link, though.
Yep as #JB said you can post the form using JScript. One example that comes to my mind is below with struts, here you are actually passing the product id as a variable to JS method (I changed the names so it matches your example).
<a href="#" onclick="removeProduct(${cartItem.productId})">
Remove
</a>
You can then have JS method to be
function removeProduct(productId) {
document.forms["formname"].elements["productId"].value =
productId;
document.forms["formname"].submitTestPost.click();
}
This also assumes the following is defined in the page (so JS can set it) and the submit property (tag SubmitTag). I am not sure in your case the setup but perhaps you can draw some points.
<input type="hidden" name="productId"/>