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

Multiple submit buttons on 1 form

Status
Not open for further replies.

butterfm

Programmer
Feb 15, 2002
132
GB
Hi,

Could somebody please tell me if its possible to have more than one submit button on a single html form or would I have to have multiple forms on a single page.

e.g.
Button 1 to sumit username and password.
Button 2 to submit a site search.

If so how do I control the action of the submit depending on which button is pressed.

I am using HTMl/ASP.

Any help for a newbie would be much appreciated.

Thanks, M.
 
what you need to do is use javascript and cahnge the input types to buttons. you can then submit from the function and have better control over what does what.
so one form:
<script>
fucntion submitTo(val) {
if(val==&quot;search&quot;) {
document.forms[0].action = &quot;search.asp&quot;
} else {
document.forms[0].action = &quot;login.asp&quot;
}

run it on the event from the buttons
<input type=&quot;button&quot; value=&quot;search&quot; onClick=&quot;return submitTo(this.value)&quot;>

____________________________________________________
The most important part of your thread is the subject line.
Make it clear and about the topic so we can find it later for reference. Please!! faq333-3811

onpnt2.gif
 
I think that you should probably use two forms on one page for this since the forms are completely different...

<form name=&quot;search&quot; action=&quot;search.asp&quot; method=&quot;post&quot;>
<input name=&quot;searchField&quot;>
<input type=submit>
</form>

<form name=&quot;login&quot; action=&quot;login.asp&quot; method=&quot;post&quot;>
<input name=userName>
<input name=pswd type=password>
<input type=submit>
</form>

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook
 
Thanks for the prompt responses guys,

These suggestions are much appreciated.

M.
 
Hi,

Another quick question. The page I'm using has nested tables to format the layout of the page and the 2 submit buttons are in the same cell. Are you allowed to do the following in html ..

<td>

<form name=&quot;search&quot; action=&quot;search.asp&quot; method=&quot;post&quot;>
<input name=&quot;searchField&quot;>
<input type=submit>
</form>

<form name=&quot;login&quot; action=&quot;login.asp&quot; method=&quot;post&quot;>
<input name=userName>
<input name=pswd type=password>
<input type=submit>
</form>

<td>


Is this bad practice and does nesting tables slow down the loading of the page.

I'm very new to this so I apologies if the questions are a bit simple.

M.
 
nothing really wrong but the fact you'll create spaces with the <form> tags in the cell that may not be what you want and throw the page design off

____________________________________________________
The most important part of your thread is the subject line.
Make it clear and about the topic so we can find it later for reference. Please!! faq333-3811

onpnt2.gif
 
you can have multiple submit buttons within one form:

just add an onClick event that changes the form's action property to the filename you want.

so (simplified) :
[tt]
<form action=&quot;&quot; name=&quot;MyForm&quot; onsubmit=&quot;someValidationFunction()&quot;>

<input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Search&quot; onClick=&quot;document.MyForm.action = 'search.asp'&quot;>
<input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Login&quot; onClick=&quot;document.MyForm.action = 'login.asp'&quot;>
[/tt]

Colm
 
If, as in this case, you're calling different scripts depending on the button that's clicked, you're much better off putting them in seperate forms. The only disadvantage (as onpnt points out) is the extra spacing this will apply to the buttons, but this can be worked around with CSS thus:
[tt]
<form name=&quot;search&quot; action=&quot;search.asp&quot; method=&quot;post&quot;>
<input name=&quot;search&quot;>
<input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Search&quot;>
</form>

<form style=&quot;margin:0&quot; name=&quot;login&quot; action=&quot;login.asp&quot; method=&quot;post&quot;>
<input name=&quot;username&quot;>
<input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Login&quot;>
</form>
[/tt]
DON'T use a Javascript function to pick what to do - it'll mean that nothing at all happens for 10% of your visitors.

-- Chris Hunt
Extra Connections Ltd

The real world's OK for a visit, but you wouldn't want to LIVE there!
 
Thanks for all the help guys.

It is much appreciated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top