Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how do I determine which submit button was clicked?

Status
Not open for further replies.

vulcand4

Programmer
Jul 29, 2003
76
US
Anyone know a way to determine which submit button was clicked (assuming there is more than one)?

I need to determine which button caused an html form to submit. I was thinking of putting the code in the onsubmit() event handler of the form itself, but I don't know how to determine which button was actually clicked.

Any help would be appreciated.

- G
 
Do you need to know this before the form is actually submitted? If you're only concerned AFTER the page has been submitted (essentially - server side) then you'd need to check for the existance of the submit button in the collection of submitted form elements. Using server side JScript and ASP here's an example:

Code:
<%
if (Request.Form("submit1").count) {
   Response.Write("submit button 1 was clicked<br />");
}
if (Request.Form("submit2").count) {
   Response.Write("submit button 2 was clicked<br />");
}
%>
<form method="post" action="">
   <input type="submit" name="submit1" value="submit button 1" />
   <input type="submit" name="submit2" value="submit button 2" />
</form>

-kaht

Looking for a puppy?

silky-icon-left.gif
[small]Silky Terriers are small, relatively odorless dogs that shed no fur and make great indoor pets.[/small]
silky-icon-right.gif
 
I need to know on the client side, before the form is submitted.

I need to check which button was clicked, then do a confirm and have the option of cancelling submission based on the results of the confirm.

-G
 
How many forms do you have on the page? If there are more than one, you can have a validation script for each form. If you have one form with more than one submit button, you need to show your form code for us to figure out how to help you better.

Lee
 
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
<head>
<title>Test</title>

<script type='text/javascript'>
function Test(str){
	alert(str);

}

</script>
</head>
<body>
<form method="post">
<input type="submit" name="one" onclick="Test('Button One')">
<input type="submit" name="two" onclick="Test('Button Two')">
</form>
</body>

</html>
 
What I was really looking for was some sort of status or property of the form field (submit button) to let me know that it was clicked.

I ended up setting a boolean var at the beginning of the HTML page and initted it to false. Then the onclick event of the button in question set the var to true.

I then used the var to determine whether the button was clicked within the onsubmit method of the form.

Thanks for all your input.

-G
 
how does a boolean var tell you which button was clicked?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
how does a boolean var tell you which button was clicked?
I was thinking the same thing [lol]

-kaht

Looking for a puppy?

silky-icon-left.gif
[small]Silky Terriers are small, relatively odorless dogs that shed no fur and make great indoor pets.[/small]
silky-icon-right.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top