Multiple Submit Button in a Single form with PHP

Do you know how to use two or more submit button in a single <form> ?

This is a critical problem while doing some professional websites!. You may face this situation in a project that needed two or multiple submit buttons!. Don’t worry this is not a Rocket Science!!. I will let you through this stuff  using PHP Programming!

See the Example Form it has two submit buttons:


<?php

if($_POST['submit1'] != '')
{
echo "You hit the button 1";
}
if($_POST['submit2'] != '')
{
echo "You hit the button 2";
}

?>

<html>
<head><title>Multiple Submit button Solved with PHP!</title></head>
<body>
<form action="" method="post">

<h2> Hit the Submit Button</h2>

<input type="submit" name="submit1" value="btn1" />
<input type="submit" name="submit2" value="btn2" />

</form>
</body>
</html>

The above program solved the problem so easily, Let me tell how this works.

Actually when you hit the first button the PHP code will check whether you hit the First Submit button or Second with the help of  “If” Condition.

We have given values to the two submit buttons in line. 21 and 22. “btn1″ and btn2”. After you hit the submit button “IF” Condition will check whether it is empty or having some value in $_POST[‘submit1’] and $_POST[‘submit2’] .

If it is not empty then it will print out the echo Statement. So easy right?. Now you can use as many as Submit Button you want, just give different values to the submit buttons and write the if conditions to do the magic!

4 Comments

Add a Comment

Your email address will not be published. Required fields are marked *