achillix
achillix

Reputation: 479

Sending POST data from one form with 2 buttons

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?

Upvotes: 2

Views: 809

Answers (4)

dfsq
dfsq

Reputation: 193261

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/

Upvotes: 2

asprin
asprin

Reputation: 9823

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);

})

Upvotes: 0

Bart Friederichs
Bart Friederichs

Reputation: 33511

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

Upvotes: 0

Quentin
Quentin

Reputation: 943510

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
    }
?>

Upvotes: 3

Related Questions