Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML form with multiple "actions"

Tags:

html

forms

submit

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.

Thanks in advance.

like image 825
Samuel Stiles Avatar asked May 21 '13 01:05

Samuel Stiles


People also ask

Can an HTML form have multiple actions?

Let's learn the steps of performing multiple actions with multiple buttons in a single HTML form: Create a form with method 'post' and set the value of the action attribute to a default URL where you want to send the form data. Create the input fields inside the as per your concern. Create a button with type submit.

Can I have two or more actions in the same form?

No, a form has only one action.

Can a HTML form have 2 submit buttons?

yes, multiple submit buttons can include in the html form. One simple example is given below.

Do HTML forms need an action?

Yes, the form is required to have an action attribute in HTML4. If it's not set, the browser will likely use the same method as providing an empty string to it. You really should set action="" which is perfectly valid HTML4, follows standards, and achieves the same exact result.


1 Answers

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:

  • 2x submit buttons to action different URL
  • Submit form to another page (which is different from the page used in ACTION)
like image 142
isaacparrot Avatar answered Sep 19 '22 16:09

isaacparrot