Send same inputs to different actions with one form Send same inputs to different actions with one form php php

Send same inputs to different actions with one form


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:


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.


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;});


That happens because you have one form with action="foo.php". It doesn't matter which button you click it will always send the data to the action of the form in this case "foo.php".
Solution:
Seperate the inputs into two forms and set the action of the second form to "excel.php". Then it should work. Hope it helped you.

Edit: Saw the second form in the last second. In this case you just have to take the input of "to" or "from" down to the second form.