Take the 2-minute tour ×

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" />

How can I achieve this?

share|improve this question
Put them in separate forms? –  user1508519 May 3 '13 at 9:41
Or use javascript or acnhor tags with query strings –  asprin May 3 '13 at 9:41
use ajax here.. –  Yogesh Suthar May 3 '13 at 9:41
Change form action on button click. –  dfsq May 3 '13 at 9:41
You can use JavaScript, to evaluate which button was pressed and changed the "action" page accordingly, before submitting. –  Orel Eraki May 3 '13 at 9:44
show 2 more comments

4 Answers

up vote 2 down vote accepted

Change form action on button click:

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


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


share|improve this answer
That is it Thank you very much –  achillix May 3 '13 at 9:59
add comment

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

 var but = $(this).val();
 var action = '';
 if(but == 'EXPORT')
    action = 'export.php';
 else if(but == 'GRAPHIQUE')
    action = 'graphique.php';

 $('form').attr('action', action);

share|improve this answer
add comment

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

share|improve this answer
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. –  Quentin May 3 '13 at 9:44
More of a comment than an answer. Should have at least given an example. –  asprin May 3 '13 at 9:50
add comment

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

    if (isset($_POST['exp'])) {
    } elseif (isset($_POST['dcb'])) {
    } else {
        // Default state / error handling
share|improve this answer
just a point. what if he wants to send the data to both the files and not just one? –  itachi May 3 '13 at 9:46
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. –  Quentin May 3 '13 at 9:47
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. –  itachi May 3 '13 at 9:52
add comment

Your Answer


