It's not a secret anymore!
The moose likes JSP and the fly likes Multiple Form Buttons Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Multiple Form Buttons" Watch "Multiple Form Buttons" New topic

Multiple Form Buttons

Sana Ali

Joined: Oct 08, 2004
Posts: 14

I am using java beans. As well as sessions.

I want to have more than 1 form buttons in one of my jsp files. For example 2 submit types called: "Back to Step 1"(input1.jsp) and "Proceed to Step 3" (input3.jsp) in a filed called input2.jsp

I want these to be in the form such that when we click "Back to Step 1" (input1.jsp) we go back one step, however, the data in the input fields on the input1.jsp is still visible, so user can edit the data.

and when after editing fields on input1.jsp the "Proceed to Step 2" (input2.jsp) button is pressed , the input2.jsp page appears, with the data we had entered before going back to input1.jsp still visible, so user can proceed with the form.

Also, i was wondering how can we have more than 2 buttons in logic,
as when we declare a form we use method="post" and action="input3.jsp"
So it seems only 1 button is supported:s. which i dont want.

But if i use 2 different forms for the two buttons the data on input1.jsp will not be visible in the form on input1.jsp.

Please help. Sorry Im a bit confused here:s Hope I have make myself cleared.

Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

"S Ali" -

Welcome to the JavaRanch! Please adjust your displayed name to meet the
JavaRanch Naming Policy. User names cannot be obviously fake and must constist of a first name and a last name.

You can change your user name here.

To sucessfully track values through your "wizard"-like forms, all you need to do is create a JavaBean that can hold the values of all the forms you will need to go through, put this bean into the session before you start progressing through the forms, and remove it only when completely done.

Forms can also have multiple submit buttons... you just give each submit button a different "name" value and check for that specific value in the request parameters of whatever resource is specified in the "action" of your form. However, for this to work, you can't specify the next page as the "action"... you really have to use the MVC approach and use a controller servlet. (Or use an MVC framework such as Struts or JSF.)

Thanks! and welcome to the JavaRanch!

Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Manoj Gundawar
Ranch Hand

Joined: Nov 17, 2002
Posts: 169
As a quick and dirty solution this can be achieved thru JavaScript too. (Although not recommended)
In JavaScript you can check what button was pressed and based on that, you can change the action servlet name (or JSP ) before submit.
Ex: If the form name is FaxForm, the code required to change the action target would like:

To show the previous page from the caches (when user submits back button) you can use following JS

[ October 08, 2004: Message edited by: Manoj Gundawar ]

Sana Ali

Joined: Oct 08, 2004
Posts: 14
would this mean the out put will be stored still in the fields, while we are moving back and forth the pages?
Daniel Hedrick

Joined: Aug 08, 2003
Posts: 10

Another solution might be to save yourself the trouble of having to travel to-and-from the server for each of the steps of your wizard...

Instead, consider using the magic of CSS and DHTML to construct each of the panels of the wizard in a separate <div> container that is hidden/shown upon proper validation of the previous panel...

1. Page loads, shows <div id='first'>
2. User fills out form elements in div#first; clicks [next] button
3. [next] button calls a validate/update function that first verifies the data and then hides div#first and shows div#second.
4. continue for as many panels as necessary.

All of the elements are contained in a single form. The last button ([save]) or whatever can then submit the form.

The primary benefit is not having to perform trips to the server just to break the content up into manageable chunks.

Two biggest downsides are:
1) It requires some work to properly deal with which <div> should be shown when the server detects errors in the form data. This isn't impossible, but it's an issue that has to be addressed.
2) If div#third has form element data that is dependent upon the content provided in div#second, this could be tricky. For example, page one selects continent, page two selects country (based on continent), page three selects state or municipality (based on country), etc.

Good luck!


There are 10 types of people in this world...<br />Those who understand binary, and those who don't.
I agree. Here's the link:
subject: Multiple Form Buttons
It's not a secret anymore!