PHP HTML Multiple Submit Buttons

From CoderLearner
Jump to: navigation, search

PHP works with Multiple submit buttons

Multiple submit buttons within a form is possible. Check the example for more detail.

Syntax Multiple Submit Buttons

<input type="submit" name="operation" value="add" />
<input type="submit" name="operation" value="subtract" />

Example 1

<?php
$total = 0;
if (!empty($_POST['operation']) && isset($_POST['operation'])) {
   switch ($_POST['operation']) {
      case 'add':
         $total = $_POST['number1'] + $_POST['number2'];
         break;
      case 'subtract':
         $total = $_POST['number1'] - $_POST['number2'];
         break;
   }
}
?>

<html>
   <head>
      <title>Multiple Submit Form</title>
   </head>
   <body>
      <b><?php echo 'Total : ' . $total; ?></b>
      <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
         <table border="0">
            <tr>
               <td>Number One:</td>
               <td><input type="text" name="number1" size="4" maxlength="4"/>
               </td>
            </tr>
            <tr>
               <td>Number Two:</td>
               <td><input type="text" name="number2" size="4" maxlength="4"/>
               </td>
            </tr>
            <tr>
               <td colspan="2">
                  <input type="submit" name="operation" value="add" />
                  <input type="submit" name="operation" value="subtract" />
               </td>
            </tr>
         </table>
      </form>
   </body>
</html>

Try It