We help IT Professionals succeed at work.

multiple submit buttons

castello
castello asked
on
I have a form with three submit buttons, each with different text.  I am calling a script indicated in the FORM tag's action attribute.  How can  I tell which submit button was clicked?  Is there a way to get the text of the button that was clicked?  
Comment
Watch Question

i think in buttons onClick event you can write the name
of the button in a hidden field and can access the value
in next page. try it.
Senior Software Developer
BRONZE EXPERT
Commented:
A snippet from the HTMLLib.


SUBMIT : is a button that when pressed submits the form. You can use the VALUE attribute to provide a non- editable label to be displayed on the button. The default label is browser-specific. If a SUBMIT button is pressed in order to submit the form, and that button has a NAME attribute specified, then that button contributes a name/value pair to the submitted data. Otherwise, a SUBMIT button makes no contribution to the submitted data.


So.

<input type="Submit" name="SubmitButton1" value="Fast">
<input type="Submit" name="SubmitButton2" value="Medium">
<input type="Submit" name="SubmitButton3" value="Slow">

should work.

You should get one of the SubmitButton variables defined in the results script when you click any of the buttons.

Regards,

Richard Quadling.

Commented:
assuming the the script you want to run is on a different page:

use the following code for the buttons
<input type="Submit" name="Submit" value="text1">
<input type="Submit" name="Submit" value="text2">
<input type="Submit" name="Submit" value="text3">

MAKE SURE THAT ALL 3 BUTTONS HAVE THE SAME NAME

on the page with the script, you will have a variable $Submit that has the value "text1", "text2", or "text3"

You can use if/then or switch/case to perform actions based on the value of $Submit
Richard QuadlingSenior Software Developer
BRONZE EXPERT

Commented:
Of course! As only 1 submit button can be pressed, they can all have the same name!

Regards,

Richard Quadling.

Author

Commented:
paul13 and RQuadling (who answered before paul13), I tested both your solutions and they both work.  These methods are easier than compustarsq's method would be.

I'm giving the points to RQuadling just because he was before paul13.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.