Reputation: 199
I have a form that posts the data using: " method="post" enctype="multipart/form-data">
The posted data has some calculations performed and the results are fed back to the form fields. The submit button is called Calculate. if(isset($_POST['Calculate']))
This works fine. However, I added another button, called Print, which I want to use to post the data for use on another page. if(isset($_POST['Print']))
Is there a method that would allow this? Basically changing the form action from PHP_SELF to newpage.php when the Print button is clicked.
Thanks.
Upvotes: 0
Views: 204
Reputation: 391
There was a solution posted using JQuery:
Jquery / Javascript change form action based on input field
I would probably do something very similar except using pure Javascript:
<script type="text/javascript">
<!--
function changeformaction(newact)
{
document.myformname.action=newact;
}
//-->
</script>
Then use something like this:
<form name="myformname" action="Calculate.php" method="POST">
<input type="submit" name="Calculate" onclick="changeformaction('Calculate.php');">
<input type="submit" name="Print" onclick="changeformaction('Print.php');">
</form>
Alernatively you could use a hidden field like this:
<form name="myformname" action="Calculate.php" method="POST" onsubmit="this.action=document.myformname.hiddenaction.value;">
<input type="submit" name="Calculate" onclick="document.myformname.hiddenaction.value='Calculate.php';">
<input type="submit" name="Print" onclick="document.myformname.hiddenaction.value='Print.php';">
<input type="hidden" name="hiddenaction" value="">
</form>
Try either.
Upvotes: 0
Reputation: 945
The only way you're going to be able to do that is to use javascript (most likely jQuery) to change the action of the form when the button is clicked.
If it were me, I would set the form action to be the one that you want for your print button. And then use jquery/ajax to call your script (or if they can be done with javascript, do them on the client side and save the post to the server) to do the calculations when that button is clicked.
Upvotes: 0