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!

Is it possible to have 2 submit button in a form? Urgetn! 1

Status
Not open for further replies.

choohean

Technical User
Jan 15, 2002
57
MY
I'm writing a page, can I have 2 different submit button? S that when I click on the different button, the form will submit into different page?

Can anyone give me some guide, is quite urgent!
 
Yes! but you have to use two form tags. One for each submit.
 
Can I do it in one form!? By clicking on one of the button, certain value will pass to certain page.
Than by click another button, certain value which is differnet from recent will pass to another page!
 
I believe not because submit passes every field within the form tag. And also because you can only specify one file to post to after the action tag.
<form action=&quot;filename&quot; method=&quot;post&quot;></form>
 
You can submit the same form to different pages with different buttons or depending on a value of the form. You can do this using client side scripting. I will give you some java examples
The form tag looks like this:
<form id=myform name=myform action=&quot;&quot;>

a submit button can look like this:
<input type =button onclick=&quot;this.enabled=false;mysubmitfunction(this);&quot;>


the javascript function:
function mysubmitfunction(objButton) {
var oktosubmit = true;
// do some client checking here if all the form elements contain valid data.
// a form element can be addressed like this:
// eval('myform.myelement').value
// if you cannot submit because some data in the form is not valid
// you have to set the oktosubmit to false
if(oktosubmit) {
myform.submit();
}
else {
// to prevent the user to click on the submit button twice really fast and submit the page twice
// the button was disabled but because the form cannot be
// submitted we have to tell the user to correct his/her input
// and give the user the opportunity to submit again.
objButton.enabled = true;
return false;
}
}
 
Sorry, I forgot to set the forms action property.

Here is the code:

if(oktosubmit) {
// here you can choose where the form should be submittied
// it could depend on a value in the form.
myform.action = &quot;someasp.asp&quot;
myform.submit();
}


 
I have tried the function given! But I really don't know how to use it!
I tried to create two buttons:
<input type =button onclick=&quot;this.enabled=false;mysubmitfunction(this);&quot;>

<input type =button onclick=&quot;this.enabled=true;mysubmitfunction(this);&quot;>

or should I create 2 different function for the different button?




 
You could do the following:

if Request.Form(&quot;cmdSubmit&quot;) = &quot;Delete&quot; then
' do delete stuff here
elseif Request.Form(&quot;cmdSubmit&quot;) = &quot;Update&quot; then
' do update stuff here
end if

<form ACTION=&quot;<%Request.ServerVariables(&quot;SCRIPT_NAME&quot;)%>&quot; METHOD=&quot;POST&quot; NAME=&quot;DataForm&quot;>

-- form stuff here --

<input type=submit name=&quot;cmdSubmit&quot; value=&quot;Update&quot;>
<input type=submit name=&quot;cmdSubmit&quot; value=&quot;Delete&quot;>

</FORM>

The trick is calling both submit buttons the same and then quiring the form as to which one was clicked on. Ladyhawk. [idea]
** ASP/VB/Java Programmer **
 
Sorry, here is the full working code. You can just use one function for both the buttons.
The inportent part is the form name property (in code myform.submit() so the form name and id has to be myform).
There is an if statement in the script on the name of the button, depending on the name of the button the action of myform will be set.
Anyway here is the code:

<script>
function mysubmitfunction(objButton) {
var oktosubmit = true;
// do some client checking here if all the form elements contain valid data.
// a form element can be addressed like this:
// eval('myform.myelement').value
// if you cannot submit because some data in the form is not valid
// you have to set the oktosubmit to false
if(oktosubmit) {
if(objButton.name==&quot;sub1&quot;) {
myform.action='sub1.asp';
}
else {
myform.action='sub2.asp';
}
myform.submit();
}
else {
// to prevent the user to click on the submit button twice really fast and submit the page twice
// the button was disabled but because the form cannot be
// submitted we have to tell the user to correct his/her input
// and give the user the opportunity to submit again.
objButton.enabled = true;
return false;
}
}
</script>
<form name=myform id=myform>
<input type=text>
<input type=button onclick=&quot;mysubmitfunction(this);&quot; name=&quot;sub1&quot; value=&quot;submit1&quot;>
<input type=button onclick=&quot;mysubmitfunction(this);&quot; name=&quot;sub2&quot; value=&quot;submit2&quot;>
</form>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top