2

How can I send the POST data from my form to two different pages depending on which post button is clicked?

My "Export" button sends the POST data to 'export.php', but the "Graphique" button must send the POST data to the 'graphique.php' page.

Here is my code:

<form name="TCAgregat" class="TCAgregat" action="export.php" method="post"> <input type="hidden" name="daco" value="<?php echo $DConso; ?>"></input> <input type="hidden" name="CNS" value="<?php echo $CNewCentres; ?>"></input> <input type=submit border="0" class="EXP" name="exp" value="EXPORT" /> <input type=submit border="0" class="DCB" name="dcb" value="GRAPHIQUE" /> </form> 

How can I achieve this?

5
  • Put them in separate forms? Commented May 3, 2013 at 9:41
  • Or use javascript or acnhor tags with query strings Commented May 3, 2013 at 9:41
  • Change form action on button click. Commented May 3, 2013 at 9:41
  • You can use JavaScript, to evaluate which button was pressed and changed the "action" page accordingly, before submitting. Commented May 3, 2013 at 9:44
  • Duplicate: Multiple submit button in a form Commented May 3, 2013 at 9:44

4 Answers 4

3

Say action="handler.php" and then write handler.php something along the lines of:

<?php if (isset($_POST['exp'])) { include('export.php'); } elseif (isset($_POST['dcb'])) { include('graphique.php'); } else { // Default state / error handling } ?> 
Sign up to request clarification or add additional context in comments.

3 Comments

just a point. what if he wants to send the data to both the files and not just one?
Then he can include both of them in the same branch of the if statement. Nothing in the question remotely suggests that he might want to though.
he google translated it probably. but the 1st line How can send the same POST data totwo different pages to me it seemes like he is trying to post on two different page at the same time.... just saying.
2

Change form action on button click:

$("form").on("click", ":submit", function(e) { $(e.delegateTarget).attr('action', $(this).data('action')); }); 

HTML:

<input type=submit data-action="export.php" value="EXPORT" /> <input type=submit data-action="graphics.php" value="GRAPHIQUE" /> 

http://jsfiddle.net/FAnq9/

Comments

0

Use JavaScript to rewrite the action of the form, then submit().

2 Comments

If you're going to do it this way (which I wouldn't recommend as it introduces an unnecessary dependency on JS), then calling submit() is redundant. Just don't cancel the default action of the form in the first place.
More of a comment than an answer. Should have at least given an example.
0

Since you've tagged it with jQuery, here is a solution:

$(input[type="submit"]).click(function(){ var but = $(this).val(); var action = ''; if(but == 'EXPORT') { action = 'export.php'; } else if(but == 'GRAPHIQUE') { action = 'graphique.php'; } $('form').attr('action', action); }) 

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.