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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

More than one submit button on a form 5

Status
Not open for further replies.

CherylD

Programmer
May 1, 2001
107
CA
I need some help with having more than one button on a form. I want each of these buttons to submit, but depending on which one is clicked, is where it submits to.

Thanks.
 
Use on onClick or onSubmit event on each button to call a function, and pass it a number or literal to indicate which button was clicked. In the function, check the passed value and, depending on what it is, change to location for the form. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Sorry that I didn't post a full example before - I had to try it out first. Here's an example of the function I was referring to:
Code:
<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
function SubmitForm(which) {
   if ( which == 1 ) {
      document.myform.action = &quot;[URL unfurl="true"]http://www.where.com/cgi-bin/pgm1.cgi&quot;;[/URL]
   } else if ( which == 2 ) {
      document.myform.action = &quot;[URL unfurl="true"]http://www.where.com/cgi-bin/pgm2.cgi&quot;;[/URL]
   } else if ( which == 3 ) {
      document.myform.action = &quot;[URL unfurl="true"]http://www.where.com/cgi-bin/pgm3.cgi&quot;;[/URL]
   }
   return true;
}
</SCRIPT>
[code]
And here's an example of three different buttons to submit to three different places:
[code]
<input type=&quot;submit&quot; name=&quot;sub1&quot; value=&quot;sub1&quot; onClick=&quot;SubmitForm(1)&quot;><br>
<input type=&quot;submit&quot; name=&quot;sub2&quot; value=&quot;sub2&quot; onClick=&quot;SubmitForm(2)&quot;><br>
<input type=&quot;submit&quot; name=&quot;sub3&quot; value=&quot;sub3&quot; onClick=&quot;SubmitForm(3)&quot;><br>
You don't need to do anything special with the form tag at all.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
i just marked your post as helpful, because i think it really is a nifty way to solve the problem. when i had a similar problem with mutiple submit buttons, i just checked inside the resulting asp page (the one specified in action attrbiute of the form) which button was acutally pressed, but it still basically submittted to the same file. ---
---
 
Thanks, Rydel. Like they say: TAMTOWTDI - There's Always More Than One Way To Do It. Either solution is valid. If the processing is going to be similar regardless of which button was pressed, its probably better to send it to the same program and sort it out there like you did. Otherwise, the solution I present might work out better. I'm sure someone else could come up with a third way to do it, too.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Thanks to both of you. I'm all set to go now!
 
A thot,

If you're going to change the action on the fly with JS, maybe have the form tag's default action lead to an error page stating that JavaScript is required for this form to function.

7 to 10% of users have JS turned off, on average.

Petey
 
So, what do you put in the form tag for the default action?

<from action&quot;?&quot;>

Or do you just leave it off?
When I try it with the action set to something it defaults to that setting. When I leave the action off completely it opens the form again like I just refreshed the screen. It doesn't seem like the script is taking over at all. What am I doing wrong?

Thanks!
 
i just have left it off, & it worked (just checked with two scriptsof my own)

just checked, it also works properly with default action defined.. Victor
 
Well.. I've been beating my head against the wall because I'm writing this code within an XML document and it's uncharted territory for me. A lot of the tags that I'm used to don't work right in XML even though it is standard HTML and Javascript embedded.

For instance: I can't make a radio button default to be checked by just typing <select type.... checked> it sees it as an undefined value. Also, I can't put the standard <!-- Begin and // End --> for Javascripting

Anyhow, I did get the script that you wrote to work except that I'm getting no rows returned on the database query that I'm doing on the HTML page and I know that it should be returning data. I'm just going to rewrite it again to see if I did something wrong.

We'll see what happens. Thanks!!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top