Reputation: 342
I am trying to use two different submit buttons in a same form. Each submit button, will call a specific servlet.
I tried to use onclick to do this, but if the submit button is out of the form, I cant receive the parameters in my servlet. Its returning null.
Is there anyway to do this?
Upvotes: 0
Views: 239
Reputation: 324790
You can have two (or more) submit buttons, however there can only be one target (there is an exception, which I will explain below). That being said, only the button that was clicked will be sent with the form data, so if you name it, you can check for its existence and call the relevant servlet.
HTML5 defines the formaction
attribute on submit buttons, which allow you to submit to a different target depending on which button is clicked. However, it is a new feature that is not supported in all browsers, which can cause problems if people are using an older version.
Ideally, you should have the form's action
attribute be a script that decides what to do with the button that was clicked, and then add formaction
attributes to the submit buttons that direct the form straight to the relevant file.
Upvotes: 0
Reputation: 12890
I believe you can just name the submit buttons differently and then check for that name on the server, after the form has been submitted. There is a way for the form action to be changed depending on the form button, which is as easy solved as Googling it would be.
See this: Two submit buttons in one form
And this: Multiple submit buttons php different actions
Upvotes: 1