This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes How to keep a button from submitting a form. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "How to keep a button from submitting a form." Watch "How to keep a button from submitting a form." New topic
Author

How to keep a button from submitting a form.

Fritz Largosa
Ranch Hand

Joined: Sep 12, 2005
Posts: 70
I have a form with two Submit buttons. Is it possible to only have one of the buttons trigger the action="Servlet", while the other does not. What I am trying to do is have only one button submit the form while the rest run methods which change my html/jsp front-end.

I cannot figure out how to keep a button from submitting a form. I only want one of the buttons to submit the form not all of them. If there is a way around this please let me know.

--------------------My code ---------------------------
<form action="Servlet" method="get">

<input type="submit" name="Submit" value="Submit">
<%
if(request.getParameter("Submit") != null){.a dynamic change occurs here..}
%>

<input type="submit" name="Submit2" value="Submit">
</form>
Anjana Ravindran
Ranch Hand

Joined: Aug 22, 2005
Posts: 76
I think you can make another button as type "button" itself so that it wont act as submit button.

eg; input type="submit" ....
u can change it as

input type="button"...

If you also want to submit the form to submit using this button of type 'button', u can write some javascript which calls on clicking this button

fnSubmitForm() {
form.submit();
}

hope this helps. reply me if u have any questions.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

One way to prevent a button from submitting a form is to not put it between the <form> and </form> tags.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60794
    
  65

Moved to the HTML forum.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60794
    
  65

Use a <button> tag with a type of other than "submit".
 
jQuery in Action, 2nd edition
 
subject: How to keep a button from submitting a form.
 
Similar Threads
submitting the invoice twice when submitt button is clicked
I know what to do but don't know how to implement it..
Pass radio button to servlet
Submit Methods pulling same data
Multiple Submit button in one jsp