Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple submit buttons php different actions

Tags:

php

submit

action

I have a website started where I want to have 2 separate submit buttons, one of which will take data entered and do some calculations to it to display on the same screen. I've got this successfully working with:

<form id="form1" name="form1" method="post" onsubmit="" onreset="" action="programname.php"> <input type="submit" name="calc" value="Find Angle"> 

and then I use:

if (!isset($_POST['submit'])){ Do actions, display calculations} 

Now I want a second submit button that still grabs the data they entered but then goes to a different address. Is there an elegant way to do this?

like image 207
Joegramming Avatar asked May 21 '12 19:05

Joegramming


People also ask

How can use multiple submit button in PHP?

Put a hidden field. And when one of the buttons are clicked before submitting, populate the value of hidden field with like say 1 when first button clicked and 2 if second one is clicked. and in submit page check for the value of this hidden field to determine which one is clicked. Show activity on this post.

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.

How do you handle multiple submit buttons in the same 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 we have 2 submit buttons in a form?

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


1 Answers

You could add an onclick method to the new submit button that will change the action of the form and then submit it.

<script type="text/javascript">   function submitForm(action) {     var form = document.getElementById('form1');     form.action = action;     form.submit();   } </script>  ...  <form id="form1">   <!-- ... -->   <input type="button" onclick="submitForm('page1.php')" value="submit 1" />   <input type="button" onclick="submitForm('page2.php')" value="submit 2" /> </form> 
like image 175
Travesty3 Avatar answered Sep 26 '22 00:09

Travesty3