0

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.

3
  • By the way, enctype="multipart/form-data" is used mainly for file attachments/uploading, so I doubt you need this, unless that's another thing you are offering in your form. Commented Sep 11, 2013 at 20:21
  • I would use jQuery and AJAX to do this instead of strictly PHP. Commented Sep 11, 2013 at 20:23
  • you can't do it at server side, use javascript instead Commented Sep 11, 2013 at 20:26

2 Answers 2

0

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.