Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send same inputs to different actions with one form

I have two input fields and I want to post the same data to two different php files, depending on whatever button is clicked.

In my case the data is going into foo.php only, but not into excel.php.

I want the data to go to excel.php, if second button is pressed.


JS:

$(function() {

  $("#from").datepicker({
    defaultDate: "+1w",
    changeMonth: true,
    numberOfMonths: 3,
    onClose: function(selectedDate) {
      $("#to").datepicker("option", "minDate", selectedDate);
    }
  });

  $("#to").datepicker({
    defaultDate: "+1w",
    changeMonth: true,
    numberOfMonths: 3,
    onClose: function(selectedDate) {
      $("#from").datepicker("option", "maxDate", selectedDate);
    }
  });

});

HTML:

<form action="foo.php" method="post">
     <label for="from">From</label>
     <input type="text" id="from" name="from" />

     <label for="to">to</label>
     <input type="text" id="to" name="to" /> <br>

     <input type="submit" value="Viewchart">
</form>

<form action="excel.php" method="post">
     <input type="submit" value="Download Excel file">
</form>
like image 969
Marium Malik Avatar asked May 16 '15 07:05

Marium Malik


People also ask

Can one 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 a form have multiple submits?

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

How do you submit form only once after multiple clicking on submit?

Disabling the Submit Button The second button however will only accept a single click and ignore all subsequent clicks. The trick is to use JavaScript to set the disabled property of the button to true. The disabled property was first introduced by Microsoft but has since been adopted as a standard by the W3C.

Can HTML form have two forms in one?

You can handle multiple separate forms on a single page by including a hidden field identifying the submitted form. You can use formsets to create multiple different instances of the same form on a single page.


2 Answers

You can change the action attribute when the button is clicked as follow:

$(function() { 
    $( ".actionable" ).click( function() {
        $('#myForm').attr('action', $(this).data("action"));
        $('#myForm').submit();
    });
});

And set the next data-* attributes to your buttons:

<form name="form" id="myForm" method="post">
    <input type="submit" class="actionable" value="Viewchart" data-action="foo.php"/>
    <input type="submit" class="actionable" value="Excel" data-action="excel.php"/>
</form>

You can see how it works here.


Related links:

  • You can read more about the data-* attributes here: http://www.w3schools.com/tags/att_global_data.asp

Clarification for OP:

The $( function() {} ); block —equivalent to $(document).ready( function() {} );— specify a function to execute when the DOM is fully loaded; you should put inside all your code which interact with the elements of the DOM.

like image 145
tomloprod Avatar answered Oct 02 '22 19:10

tomloprod


It can be done in your way by next script and little change in second form declaration

for second form you should set id:

<form id="form2" action="excel.php" method="post">

And you should add the next script:

$("#form2").submit( function(eventObj){
$(this).append('<input type="hidden" name="from" value="'+$("#from").val()+'" /> ');  
$(this).append('<input type="hidden" name="to" value="'+$("#to").val()+'" /> ');               
return true;
});
like image 38
Kancho Iliev Avatar answered Oct 02 '22 19:10

Kancho Iliev