Multiple Submit Buttons on Forms: Part II

This is Part II in a discussion on how to incorporate multiple submit buttons on html forms.

Part I focused on having multiple submit buttons that performed completely different actions, such as going to different web pages. Here we consider multiple submit buttons that launch the same php application, but take different actions within that application.

Here we have a simple form with three submit buttons:

<form name=”categories” method=”get” action=”action.php”>
<input type=”submit” name=”Submit_1″ value=”Submit”>
<input type=”submit” name=”Submit_2″ value=”Submit”>
<input type=”submit” name=”Submit_3″ value=”Submit”>
</form>

I am using the get method so that you can see what actually gets sent along with the url.
When you click on Submit_1 you see this URL:

http://www.urlkazoo.com/action.php?Submit_1=Submit

You can see that it is passing the name of the Submit button as a parameter. To use this information, one simply needs to identify which parameter is being passed. We can do this in the php code in action.php. Here is a php snippet:

// Test is Submit_1 is defined
if (isset($_REQUEST[“Submit_1”]))
{
$button = 1;
}
// Test is Submit_2 is defined
if (isset($_REQUEST[“Submit_2”]))
{
$button = 2;
}
// Test is Submit_3 is defined
if (isset($_REQUEST[“Submit_3”]))
{
$button = 3;
}

Of course the code above can be simplified using else statements.
But the point is to demonstrate that we can determine which button was pressed by figuring out which button’s name has been passed along as a parameter.

From here you can go on to perform different functions based on which submit button was pressed.

Good Luck!

Be Sociable, Share!

Leave a Reply

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