0

I currently have a page that has a form such as this:

<form method="post">
    <input name="data1" type="hidden" value="x">
    <input name="data2" type="hidden" value="y">

    <button name="button1" formaction="page1.php">Do something</button>
    <button name="button2" formaction="page2.php">Do something else</button>

</form>

To tidy this up I would like to consolidate page1 and page2 into one but I would need to be able to identify which button fired it. So I would have something like this:

<form method="post">
    <input name="data1" type="hidden" value="x">
    <input name="data2" type="hidden" value="y">

    <button name="button1" formaction="page.php">Do something</button>
    <button name="button2" formaction="page.php">Do something else</button>
</form>

Is it possible for page.php to know which button fired it?

RGriffiths
  • 5,722
  • 18
  • 72
  • 120
  • possible duplicate of http://stackoverflow.com/questions/2680160/how-can-i-tell-which-button-was-clicked-in-a-php-form-submit – ADyson Nov 10 '16 at 11:12
  • Change the buttons to inputs with type="submit", when the form is submitted check the post array for the button name. – Blinkydamo Nov 10 '16 at 11:13

4 Answers4

2

There's nothing to change in your <form /> here, thour PHP script will know which button has been clicked thanks to the name attribute :

<?php
if (isset($_POST['button1'])) {
    echo 'button1 has been clicked!';
} else if (isset($_POST['button2'])) {
    echo 'button2 has been clicked!';
}
roberto06
  • 3,844
  • 1
  • 18
  • 29
1

You can do it through javascript.

Give same class to both buttons and give unique ids.

On click of buttons, call jQuery.

Get the form action from respective button.

Set it to <form>'s action.

Submit the form.

Note: For understanding purpose, the alert is added.

<script>
$(function(){
    $(".sub").on("click", function(){
        var id = $(this).attr("id");
        var formAction = $(this).attr("formaction");
        $("#msg").html(id + " button clicked and form will be submitted to: " + formAction);
        alert(id + " button clicked and form will be submitted to: " + formAction);
        $("#frm").attr("action", formAction);
        $("#frm").submit();
        //return false;
    });
});
</script>
<form method="post">
    <input name="data1" type="hidden" value="x">
    <input name="data2" type="hidden" value="y">

    <button name="button1" formaction="page1.php" class="sub" id="one">Do something</button>
    <button name="button2" formaction="page2.php" class="sub" id="two">Do something else</button>
    <div id="msg"></div>
</form>
Pupil
  • 23,834
  • 6
  • 44
  • 66
0

Alter the buttons to submits and check for the input name in the post array in the php file.

<form method="post">
    <input name="data1" type="hidden" value="x">
    <input name="data2" type="hidden" value="y">

    <input type="submit" value="Do Something" name="button1" formaction="page.php" />
    <input type="submit" value="Do Something Else" name="button2" formaction="page.php" >
</form>

PHP Script

if( isset( $_POST[ 'button1' ] ) )
{
  // Do something
}
elseif( isset( $_POST[ 'button2' ] ) )
{
  // Do something else
}
Blinkydamo
  • 1,582
  • 9
  • 20
  • Actually, no need to transform `button` into `input type="submit"`. Plus, you have a syntax error (no closing `"` after `type="submit`) – roberto06 Nov 10 '16 at 11:18
0

Pass the id in the button. Yes it is possible. when collecting the value do use ::$value = $_POST ; print_r($value) ;

and check whcih button has triggered