Search code examples
htmlformssubmit

HTML form with multiple "actions"


I'm setting up a form wherein I need two "actions" (two buttons):

1 - "Submit this form for approval"
2 - "Save this application for later"

How do I create an HTML form that supports multiple "actions"?

EG:

<form class="form-horizontal" action="submit_for_approval.php">
<form class="form-horizontal" action="save_for_later.php">

I need to combine these two options-for-submitting into one form.

I did some basic research but couldn't find a definitive answer as to whether or not this is possible, and/or any good resources to links for a workaround.


Solution

  • As @AliK mentioned, this can be done easily by looking at the value of the submit buttons.

    When you submit a form, unset variables will evaluate false. If you set both submit buttons to be part of the same form, you can just check and see which button has been set.

    HTML:

    <form action="handle_user.php" method="POST" />
      <input type="submit" value="Save" name="save" />
      <input type="submit" value="Submit for Approval" name="approve" />
    </form>
    

    PHP

    if($_POST["save"]) {
      //User hit the save button, handle accordingly
    }
    //You can do an else, but I prefer a separate statement
    if($_POST["approve"]) {
      //User hit the Submit for Approval button, handle accordingly
    }
    

    EDIT


    If you'd rather not change your PHP setup, try this: http://pastebin.com/j0GUF7MV
    This is the JavaScript method @AliK was reffering to.

    Related: