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>
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.
yes, multiple submit buttons can include in the html form. One simple example is given below.
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.
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.
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.
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.
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;
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With