How to use multiple submit buttons on a single form

Generally, an HTML form takes only one submit button to send data.

If you want to know how to use multiple submit buttons on a single form, just follow this little ‘tutorial’.

First, you should create a form with 3 (or more, it’s up to you) submit buttons. Note that the 3 submit buttons have the same name. The only difference is the value attribute with unique values.

<FORM action=”mulsub.php” method=”post”>
First name: <INPUT type=”TEXT” name=”FNAME”><BR>
<INPUT type=”submit” name=”bsubmit” value=”Submit 1″>

<INPUT type=”submit” name=”bsubmit” value=”Submit 2″>
<INPUT type=”submit” name=”bsubmit” value=”Submit 3″>
</FORM>

Here, we assume that the script we are calling to handle the form submission is mulsub.php

Here is the code of the mulsub.php script

<HTML><BODY>
<?php
$btnID = “”;

if (isset($_POST[”bsubmit”]))
{
if ($_POST[”bsubmit”]==”Submit 1″)
$btnID = 1;
else if ($_POST[”bsubmit”]==”Submit 2″)
$btnID = 2;
else
$btnID = 3;
}

?>
Submit button ID: <?php echo $btnID; ?>
</BODY></HTML>

Here, we are only checking the value of the buttons named bsubmit and performing the appropriate action within the script.

Finally, let’s combine the 2 scripts into a single file, named index.php

<HTML>
<BODY>

<?php
$btnID = “”;

if (isset($_POST[”bsubmit”]))
{
if ($_POST[”bsubmit”]==”Submit 1″)
$btnID = 1;
else if ($_POST[”bsubmit”]==”Submit 2″)
$btnID = 2;
else
$btnID = 3;
}

?>
Submit button ID: <?php echo $btnID; ?><BR>

<FORM action=”<?php $_SERVER[”PHP_SELF”]?>” method=”post”>
First name: <INPUT type=”TEXT” name=”FNAME”><BR>
<INPUT type=”submit” name=”bsubmit” value=”Submit 1″>
<INPUT type=”submit” name=”bsubmit” value=”Submit 2″>
<INPUT type=”submit” name=”bsubmit” value=”Submit 3″>
</FORM>

</BODY>
</HTML>

Hope you have enjoyed this tutorial.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Wists
  • MisterWong
  • DZone
  • Technorati
  • YahooMyWeb

About this entry