How to use two submit button in one html form

In real time applications may chance come to use two submit button in one html form.For example in user registration application,having one html form named userpolicy.html,it getting user information and displaying user policy also it contains two submit button like Agree and Disagree.When user click Agree button his/her information will be stored in database.When user click Disagree it will go to home or any other file.So we have to find which submit button is clicked by user.Here is the solution to find user clicked submit button.

<!-- userpolicy.html -->
<html>
<body>
<form action="process.php" method="POST">
	Name <input type="text" name="username"> 
	Password <input type="password" name="password">
	<!-- User policy goes here -->
	<!-- two submit button -->
	<input type="submit" name="agree" value="Agree">
	<input type="submit" name="disagree" value="Disagree">
</form>
</body>
</html>
/* Process.php */ 
<?php
if($_POST['agree'] == 'Agree') {
	$username = $_POST['username'];
	$password = $_POST['password'];
 /* Database connection goes here */
}
else {
	header("Location:http://user/home.html");
}
?>

By using javascript also we can achieve this.

<html>
<head>
<script language="javascript">
function funAgree(){
 document.form1.action="addDB.php";
}
function funDisagree(){
 document.form1.action="home.html";
}
</script>
</head>
<body>
<form name="form1" action="" method="POST">
	Name <input type="text" name="username"> <br>
	Password <input type="password" name="password"> <br>
	<!-- User policy goes here -->
	<!-- two submit button -->
	<input type="submit" name="agree" value="Agree" onclick="funAgree()">
	<input type="submit" name="disagree" value="Disagree" onclick="funDisagree()">
</form>
</body>
</html>
Like Loading...
Posted in PHP

27 thoughts on “How to use two submit button in one html form

  1. Thank you for code, it works well, i am talking about 2 submit’s with javascript. You save my time to mess around. Thank you again.

    Zygis

  2. im looking this code for a couple of weeks..i cant imagine the solution is using function. thanks a lot bro. i thought its using secondary form for posting value..its really helped me a lot πŸ™‚

  3. just for informing you, your code is not detectable by IE6 !
    and it just works for ie7+
    i’m looking for the right way for IE6, i’ll post a comment as soon as i found a way

  4. thx it helps me a lot..keep up value of sharing to others..may god bless u alwayz and protect you from danger

Leave a comment Cancel reply