One Form, Multiple Actions

November 16th, 2005

Not everyone is aware of this fact, so I thought I'd share it. You can have a single form, with multiple submit buttons, and then determine which button was clicked as to what action to take. Take the following code:

<form action="test.php" method="post"> <input type="submit" name="edit" value="edit" /><br />
<input type="submit" name="delete" value="delete" /><br />
</form>

If your user clicks on the "edit" button, then $_POST["edit"] will be available, and the same if they click on the "delete" button. You can test for these buttons within your PHP code, and then perform the related actions:

PHP:
  1. if(isset($_POST["edit"]) && trim($_POST["edit"])!=='') {
  2. //they clicked edit
  3. //perform INSERT SQL Query
  4. } elseif(isset($_POST["delete"]) && trim($_POST["delete"])!=='') {
  5. //they clicked delete
  6. //perform DELETE SQL Query
  7. } else {
  8. //nothing has been clicked
  9. //load initial form
  10. }

You can of course have as many buttons as you wish - thus having as many actions as necessary.


 Add to del.icio.us    Digg this    Technorati

Related Posts:

Entry Filed under: PHP

2 Comments Add your own

  • 1. Chris H  |  November 19th, 2005 at 1:02 am

    Cool!

    Never really considered that. Consider it cut n pasted.

  • 2. misiek  |  April 27th, 2007 at 3:08 pm

    How to do in ajax.
    I need to sibmit ajax form, this way it return whole params, for all buttons

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Calendar

November 2005
M T W T F S S
    Dec »
 123456
78910111213
14151617181920
21222324252627
282930  

Most Recent Posts