Unstoppable Domains

How to Handle more than one submit button in single form?

Spaceship Spaceship
Watch

asparsh

New Member
Impact
0
Hi
I have one PHP file which contains one Form. In this form there are two Submit type Buttons
<input type="submit" name="edit" value="save">
<input type="submit" name="cancel" value="cancel">
<form name="myform" onsubmit="return validate()">

My problem is : When i m trying to use Javascript onsubmit event of a form it confused due to two submit buttons and onsubmit of any button(save or cancel) it runs Javascript. Generally it should not run on cancel button.

Please provide solution as soon as possible.

thanks in advance.

Anish Panchal
 
Last edited by a moderator:
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
.US domains.US domains
I would probably do something like
HTML:
<input type="button" value="cancel" onclick="doCancelStuff()"/>
I'm assuming you don't really need to submit the form if the user is canceling.

Tip: if you're writing XHTML you should self-close your input tags. :)
 
0
•••
The problem here is that your second submit button shouldn't be a submit at all. It should be a reset. Like so:

Code:
<input type="reset" value="reset" />
 
Last edited by a moderator:
0
•••
Not sure what do you want to achieve from the Cancel button. But here are the scenarios I can think of.

Scenario 1: You want to take the user back to the previous page when they click Cancel.
Solution: <input type="button" value="Cancel" onclick="javascript:history.back();" />

Scenario 2: As DomainManDave said, you want to reset the form fields
Solution: Refer to DomainManDave's reply

Scenario 3: You really want to do a real action when users click Cancel
Solution:
Code:
<input type="submit" name="sbmt" value="save">
<input type="submit" name="sbmt" value="cancel">

in the action page, do something like this:

Code:
if(isset($_POST['sbmt']))
{
if($_POST['sbmt'] == 'save'){
// code here
}
elseif($_POST['sbmt'] == 'cancel'){
// code here
}
}
 
0
•••
The above code is not valid though. You cannot have to items with the same name.
 
0
•••
0
•••
You cannot have two submit buttons. One should just be input type="button" and the onclick function can be set to do whatever you'd like.
 
0
•••
Of course you can have more than one submit button. They should have different names so you can distinguish which one was pressed.

I would avoid relying on javascript if possible.
 
0
•••
I don't know why you guys make an argument before you verify the solution.

Here is the page
http://www.syspire.com/temp/submit.php

and here is the code:

Code:
<?php
if(isset($_POST['sbmt'])){
	if($_POST['sbmt'] == 'Write Hello'){
		echo 'Hello';
	}
	elseif($_POST['sbmt'] == 'Write World'){
		echo 'World';
	}
}
?>

<form method="post">
<input type="submit" name="sbmt" value="Write Hello" />
 
<input type="submit" name="sbmt" value="Write World" />
</form>
 
0
•••
Dynadot — .com TransferDynadot — .com Transfer
Appraise.net

We're social

Domain Recover
DomainEasy — Zero Commission
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back