最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

forms - How to know what submit button the user clicked?

programmeradmin3浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论