PHP: Multiple submit button choices in a single FORM

This one is just posted mainly for a friend. He did not understand how to process data differently depending on the submit button clicked, only using ONE HTML FORM. Many people would end up making multiple forms on one or many pages to solve this, which can get rather messy or annoying.

There is a simple way to handle this though using PHP. Take this example:

<input name="button1" type="submit" value="You hit button 1" />
<input name="button2" type="submit" value="You hit button 2" />
<input name="button3" type="submit" value="You hit button 3" />
<?php
// check which button was clicked
if ($_POST['button1'] {
    echo "Button one clicked!";
} else if ($_POST['button2']) {
    echo "Button two clicked!"
} else if ($_POST['button3']) {
    echo "Button three clicked!";
}
?>

You can see the above in action HERE! As you see, it is a SIMPLE process that will recognize what button you click with very little coding. Using this method, you can process data differently in one simple form! Easy!

In the next few days I’ll probably be working on a small login system that Leigh, a friend of mine requested too. If you also wish for any help/ideas feel free to leave a post or comment and I can normally whip up something within a day or two if it’s a small thing.

Share

Related posts:

  1. Lightbox Gone Wild – Post/Get Form Submission Issues?
  2. PHP: GIF animation splitter (Split any GIF animiations – frame by frame properly!)
  3. PHP: How to get a random image to display from a specific folder
  4. PHP: Randomizing a text file using PHP + MYSQL, quickly & efficient

About the Author

I mainly focus on Javascript/PHP/C++/.NET applications for everyday and work. I also am working on a remake of Stellar Frontier, an old 2D top down space battle game with a few fellow programmers.