Srikanth Koneru
Srikanth Koneru

Reputation: 264

How to know which button was clicked if a form has two buttons

I have two forms, one with login and another with logout and they both use the same controller/form processor, I am using

$_SERVER['REQUEST_METHOD'] 

to see if the form is submitted.

But how can I know if login or logout was clicked.

$_POST['action'] == 'login'

and

$_POST['action'] == 'logout' 

are not working.

okay here is the complete form :

<?php

$test = 'default';
if( $_SERVER['REQUEST_METHOD'] == 'POST' && $_POST['action'] == 'login' ){
    //do some stuff
    $test = 'login';
}elseif( $_SERVER['REQUEST_METHOD'] == 'POST' && $_POST['action'] == 'logout' ){
    //do other stuff
    $test = 'logout';
}
?><?php echo $test; ?>
            <form method="post" action="">

                    <p>
                    <input type="text" name="username" placeholder="Username" value="">
                    </p> 

                    <p>
                    <input type="password" name="password" value="" placeholder="Password">
                    </p>

                    <p>
                    <input type="submit" name="login" value="Log In">
                    </p>                                                               


            </form>

            <form method="post" action="">
                <input type="submit" name="logout" value="Log Out">
            </form>

But the $test doesn't change.

Upvotes: 0

Views: 506

Answers (3)

GolezTrol
GolezTrol

Reputation: 116110

Give the buttons different names

If you have a input element of type submit, you will get a button that posts a value itself. Give it a name and a value, and that value will be posted along with the post data:

<input type="submit" name="button1" value="Click me">
<input type="submit" name="button2" value="Or me">

The value is often localized and I think you wouldn't want to check for that, but you can just check for the existence of Button1 or Button2 in the post variables to see which one was clicked, regardless what their value is.

Give the buttons (only) different values

Alternatively, if you know the button value is useful (it contains an id or name rather than a localized text from a template), you could give both buttons the same name (like 'action') and check the value of the post variable instead. In that case, the two buttons behave more or less like a group of radio buttons. Not my preference, but certainly possible and acceptable.

<input type="submit" name="action" value="Action 1">
<input type="submit" name="action" value="Action 1">

Or you could use a button tag in that case, which is a similar but slightly different element. It also has a name and value attribute, but you can specify the text as the content of the element, so you can decouple the value from the text you present to the user. This would be better than using the input version above.

<button type="submit" name="action" value="Action 1">Click me for action 1!</button>
<button type="submit" name="action" value="Action 2">Secondary action</button>

Note that type="submit" is the default for buttons, so you can omit it, as long as you don't need to support IE7.

Checking which one was clicked

Whichever solution you pick, don't forget to check thoroughly though. There are other ways of submitting a form, for instance through clicking enter, so make sure to propertly handle the case where neither button was clicked.

if (array_key_exists('button1', $_POST)) {
  // Button 1 was clicked
} elseif (array_key_exists('button2', $_POST)) {
  // Button 2 was clicked
} else {
  // Neither was clicked. 
}

or for the alternative

if (array_key_exists('action', $_POST)) {

  switch ($_POST['action') {
    case 'Action 1': 
      // Button 1 was clicked
      break;
    case 'Action 2': 
      // Button 2 was clicked
      break;
    default:
      // An unknown button was clicked!
      break;
  }

} else {
  // Neither was clicked. 
}

Upvotes: 3

Waqas Shahid
Waqas Shahid

Reputation: 1060

You can identify through isset function:

if(isset($_POST['action']) && $_POST['action']=="login"){
  //Login Button Logic
} else if(isset($_POST['action']) && $_POST['action']=="logout"){
  //Logout Button Logic
}

Upvotes: 1

Mihai Matei
Mihai Matei

Reputation: 24276

Assign names to the buttons:

<input type="submit" name="Add" value="Add" />
<input type="submit" name="Delete" value="Delete" />

and verify which one is set as:

if (isset($_POST['Add'])) {
    // add
} elseif (isset($_POST['Delete'])) {
    // delete
}

Upvotes: 1

Related Questions