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?
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" />
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
}
?>
Use JavaScript to rewrite the action
of the form, then submit()
.
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);
})