admin管理员组

文章数量:1386662

I'm working on my settings page, I want to add two different buttons to the form and do something different depending which the user clicked.

The documentation states I can create two buttons like this:

submit_button('Submit', 'primary large', 'primary', false);
submit_button( 'Delete', 'delete', 'delete', false );

However I don't understand how to differenciate when the user clicked each one, it seems that no matter which button clicked the form is submitted the same.

How can I do something different depending what button the user clicks?

I'm working on my settings page, I want to add two different buttons to the form and do something different depending which the user clicked.

The documentation states I can create two buttons like this:

submit_button('Submit', 'primary large', 'primary', false);
submit_button( 'Delete', 'delete', 'delete', false );

However I don't understand how to differenciate when the user clicked each one, it seems that no matter which button clicked the form is submitted the same.

How can I do something different depending what button the user clicks?

Share Improve this question asked Jan 22, 2015 at 15:46 Lisandro VaccaroLisandro Vaccaro 9954 gold badges12 silver badges28 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

The submit_button() function is a wrapper for get_submit_button(). Now that function has multiple arguments, but the most interesting for your actual problem is the 3rd argument name. It sets the HTML name attribute.

<form action="">
    <input ... etc.
    <button type="submit" name="choice-a">
</form>

Now everytime you process your form with an empty action attribute, you will point to the current request http://example/wp-admin/example.php. There you will be able to fetch your arguments via the super globals $_POST or $_REQUEST (which simplified is mostly a combination of $_GET and $_POST).

Then just inspect the array and do whatever you need/want to do.

本文标签: formsHow to know what submit button the user clicked