• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to detect which button is clicked in a FORM from server side ?

 
Artemesia Lakener
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
originally posted a thread asking "do I need to use javascript to put multiple buttons on a form ?" many people said yes and it was moved to "HTML" section. However, I read one response thread and it says it can also be handled by the server side using servlet. I don't know how. Could someone tell me how to use servlet to find out which action button is clicked and how to pass different action names ? any short sample will be great.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use a hidden field for this purpose. Just assign a unique value to every button and pass that value to that hidden field, on button click. Check that hidden/parameter on your server and you will know which button was actually pressed.

thanks.
 
D Rog
Ranch Hand
Posts: 472
Linux Objective C Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should get button name value. If it's image button, then you should get button name dot x and y coordinates.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are several methods.

One is to give all of your buttons the same name.
If you do so, only the one clicked will be sent to the server.

Then in your servlet code, you can read parameter and branch on it's value.


The problem with this approach is that your controller logic is dependent on your view (look and feel). At some point a client, manager, or web designer will want the buttons to say "save" instead of "enter". At this point, you will need to change your logic in the servlet to match the value of the buttons.

For this reason, I prefer to give each button a separate name and test to see which is not null in the servlet.


Then in the servlet.



Now management can change the value of the buttons till their heart's content without breaking your servlet code.

The third option, which D Rog mentioned, is to use input fields of type "image". The visible part of an image tag is the image itself or whatever is in your "alt" attribute if the image can't be found. If you're using images, you can give them all the same name and read the value like in the first example. The value never gets printed to the page so there is no need for anyone to change it.
[ August 03, 2005: Message edited by: Ben Souther ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64724
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yet another option is to not switch on the server side at all. If you like to keep the server-side code as UI-unbound as possible, I'd have each button submit to a completely different action rather than having a single action that needs to act as a traffic cop depending upon the button name.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic