Asked  7 Months ago    Answers:  5   Viewed   40 times

I have several buttons on my page, but I'm not sure how to tell which one was clicked. Here's the markup for my two buttons:

<input type="submit" id="btnSubmit" value="Save Changes" />
<input type="submit" id="btnDelete" value="Delete" />

 Answers

32

With an HTML form like:

<input type="submit" name="btnSubmit" value="Save Changes" />
<input type="submit" name="btnDelete" value="Delete" />

The PHP code to use would look like:

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Something posted

    if (isset($_POST['btnDelete'])) {
        // btnDelete
    } else {
        // Assume btnSubmit
    }
}

You should always assume or default to the first submit button to appear in the form HTML source code. In practice, the various browsers reliably send the name/value of a submit button with the post data when:

  1. The user literally clicks the submit button with the mouse or pointing device
  2. Or there is focus on the submit button (they tabbed to it), and then the Enter key is pressed.

Other ways to submit a form exist, and some browsers/versions decide not to send the name/value of any submit buttons in some of these situations. For example, many users submit forms by pressing the Enter key when the cursor/focus is on a text field. Forms can also be submitted via JavaScript, as well as some more obscure methods.

It's important to pay attention to this detail, otherwise you can really frustrate your users when they submit a form, yet "nothing happens" and their data is lost, because your code failed to detect a form submission, because you did not anticipate the fact that the name/value of a submit button may not be sent with the post data.

Also, the above advice should be used for forms with a single submit button too because you should always assume a default submit button.

I'm aware that the Internet is filled with tons of form-handler tutorials, and almost of all them do nothing more than check for the name and value of a submit button. But, they're just plain wrong!

Wednesday, March 31, 2021
 
Easen
answered 7 Months ago
86

You should point to your vendor/autoload.php at Settings | PHP | PHPUnit when using PHPUnit via Composer.

This blog post has all the details (with pictures) to successfully configure IDE for such scenario: http://confluence.jetbrains.com/display/PhpStorm/PHPUnit+Installation+via+Composer+in+PhpStorm

Related usability ticket: http://youtrack.jetbrains.com/issue/WI-18388

P.S. The WI-18388 ticket is already fixed in v8.0

Wednesday, March 31, 2021
 
ojrac
answered 7 Months ago
79

On Mac OS X environment variables available in Terminal and for the normal applications can be different, check the related question for the solution how to make them similar.

Note that this solution will not work on Mountain Lion (10.8).

Saturday, May 29, 2021
 
Nate
answered 5 Months ago
90

For sending out the actual e-mail I would recommend using the PHPMailer library, it makes everything much easier.

Thursday, July 29, 2021
 
Isky
answered 3 Months ago
34

Implement View's OnClickListner in your activity class. Override on click method.

 Button b1= (Button) findViewById(R.id.button1);
//find your button id defined in your xml. 


b1.setOnClickListener(this);
// You have button OnClickListener implemented in your activity class.
//this refers to your activity context.

I have used a toast message.

http://developer.android.com/guide/topics/ui/notifiers/toasts.html

 Toast.makeText(MainActivity.this,"button1", 1000).show();
 //display a toast using activity context ,text and duration

Using switch case you can check which button is clicked.

In your onClick method.

switch(v.getId())  //get the id of the view clicked. (in this case button)
{
case R.id.button1 : // if its button1
    //do something
    break;
}

Here's the complete code.

public class MainActivity extends Activity implements OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button b1= (Button) findViewById(R.id.button1);
    Button b2= (Button) findViewById(R.id.button2);
    Button b3= (Button) findViewById(R.id.button3);
    b1.setOnClickListener(this);
    b2.setOnClickListener(this);
    b3.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch(v.getId())
    {
    case R.id.button1 :
        Toast.makeText(MainActivity.this,"button1", 1000).show();
        break;
    case R.id.button2 :
        Toast.makeText(MainActivity.this,"button2", 1000).show();
        break;
    case R.id.button3 :
        Toast.makeText(MainActivity.this,"button3", 1000).show();
        break;  


    }

}
 }
Thursday, July 29, 2021
 
jsuggs
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share