Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One form with two submit buttons and different actions for each button

Tags:

forms

php

I have spent several hours trying to find a solution to my issue, but just can't seem to find the proper solution. Thanks in advance for your assistance!

I have ONE html form with:

<form id="columnarForm"  action="formindb_hoh_1.php" enctype="multipart/form-data" method="post" /> 

I would like to have TWO submit buttons:

<input type="image" name="camper" value="camper" src="../images/apps/camperBtn.png" class="submit_button" /> <input type="image" name="medical" value="medical" src="../images/apps/medicalBtn.png"class="submit_button" /> 

I would like the first submit button to have the action of formindb_hoh_1.php when clicked and the second submit button to have the action of formindb_hoh_2.php, but am unsure how to make one button have one action and have the other button has a different action.

like image 820
Nick Avatar asked Feb 08 '13 03:02

Nick


People also ask

Can we have 2 submit buttons in a form?

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

How do I use multiple submit buttons in one form?

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 you have multiple submit buttons in a form PHP?

Processing form data in PHP is significantly simpler than most other Web programming languages. This simplicity and ease of use makes it possible to do some fairly complex things with forms, including handling multiple submit buttons in the same form.


2 Answers

Refer this :

Multiple submit buttons php different actions

Put this script in your page :

<script>     function submitForm(action)     {         document.getElementById('columnarForm').action = action;         document.getElementById('columnarForm').submit();     } </script> 

Modify your input code :

<input type="image" name="camper" onclick="submitForm('formindb_hoh_1.php')" value="camper" src="../images/apps/camperBtn.png" class="submit_button" /> <input type="image" name="medical" onclick="submitForm('formindb_hoh_2.php')" value="medical" src="../images/apps/medicalBtn.png"class="submit_button" /> 
like image 24
Redzwan Latif Avatar answered Sep 19 '22 13:09

Redzwan Latif


No JS, use HTML5.

Change the submit buttons to use new HMTL5 button attribute formaction:

<button type="submit" name="camper" formaction="formindb_hoh_1.php">Camper</button> <button type="submit" name="camper" formaction="formindb_hoh_2.php">Medical</button> 

Reference: http://devdocs.io/html/element/button

like image 55
szuflad Avatar answered Sep 18 '22 13:09

szuflad