# Cake 4: Check for button click in controller

**URL:** <https://discourse.cakephp.org/t/cake-4-check-for-button-click-in-controller/9233>\
**Category:** Need Help\
**Tags:** help\
**Created:** [April 13, 2021, 11:32am UTC](https://discourse.cakephp.org/t/cake-4-check-for-button-click-in-controller/9233 "2021-04-13T11:32:32Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![die7fox](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/die7fox/32/1970_2.png) [@die7fox](https://discourse.cakephp.org/u/die7fox)\
**Post date:** [April 13, 2021, 11:32am UTC](https://discourse.cakephp.org/t/cake-4-check-for-button-click-in-controller/9233/1 "2021-04-13T11:32:32Z")

</div>

In Cake 2.x, I could simply do `if (isset($this->request->data['button_name']))` if I wanted to verify that a certain form button was clicked. What’s the Cake 4 equivalent if I have a button configured like this?

```
<?= $this->Form->button('Export to Excel', ['type' => 'submit', 'name' => 'export', 'id' => 'export']) ?>
```

---

<div class="post-metadata">

**Author:** ![dirk](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dirk/32/2174_2.png) [@dirk](https://discourse.cakephp.org/u/dirk)\
**Post date:** [April 13, 2021, 12:03pm UTC](https://discourse.cakephp.org/t/cake-4-check-for-button-click-in-controller/9233/2 "2021-04-13T12:03:40Z")

</div>

> [@die7fox](#):
>
> $this-\>request-\>data[‘button\_name’

please try this:

```
$button = $this->request->getData('export');
          if (isset($button)) {
            //your code
          }

```

---

<div class="post-metadata">

**Author:** ![die7fox](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/die7fox/32/1970_2.png) [@die7fox](https://discourse.cakephp.org/u/die7fox)\
**Post date:** [April 13, 2021, 12:12pm UTC](https://discourse.cakephp.org/t/cake-4-check-for-button-click-in-controller/9233/3 "2021-04-13T12:12:12Z")

</div>

No joy, alas. Debug of `$button` upon submit shows `null`, so it looks like the value isn’t getting to my `export()` method.

Further background: I have three buttons on this particular form : Search, Reset, and Export to Excel. The third one lets the user export the result set from the search after Search is clicked. The `search()` method passes `$query` results to `export()`, which I want to trigger with Export to Excel. In Cake 2.x, this was a cinch. I did something similar to what you suggested.

For some reason, `export()` isn’t recognizing the button click.

---

<div class="post-metadata">

**Author:** ![dirk](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dirk/32/2174_2.png) [@dirk](https://discourse.cakephp.org/u/dirk)\
**Post date:** [April 13, 2021, 12:26pm UTC](https://discourse.cakephp.org/t/cake-4-check-for-button-click-in-controller/9233/4 "2021-04-13T12:26:29Z")

</div>

I have in my form:

> ```
> <?= $this->Form->button(__('Submit'),['name'=>'export']) ?>
> <?= $this->Form->button(__('Submit'),['name'=>'search']) ?>
> <?= $this->Form->button(__('Submit'),['name'=>'reset']) ?>
> 
> ```

and can catch each button in my controller

> ```
> if ($this->request->is(['patch', 'post', 'put'])) {     
> $export = $this->request->getData('export');
> if (isset($export)) {
> debug($this->request->getData());
> }
> $search = $this->request->getData('search');
> if (isset($search)) {
> debug($this->request->getData());
> }
> $reset = $this->request->getData('reset');
> if (isset($reset)) {
> debug($this->request->getData());
> }
> 
> ```
> 
> //Rest of Code  
> }

and it works

---

<div class="post-metadata">

**Author:** ![die7fox](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/die7fox/32/1970_2.png) [@die7fox](https://discourse.cakephp.org/u/die7fox)\
**Post date:** [April 13, 2021, 12:35pm UTC](https://discourse.cakephp.org/t/cake-4-check-for-button-click-in-controller/9233/5 "2021-04-13T12:35:03Z")

</div>

Interesting. What are you getting back when you debug? When I debug any button in either `search()` or `export()`, I get back `null.` The Export button also just resubmits my search form instead of triggering `export()`. In my Cake 2.x app, I handled that in my `search()` method by checking for which button was clicked and just didn’t render a view if it was Export. Here, I can’t even get the buttons to produce a value.

---

<div class="post-metadata">

**Author:** ![dirk](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dirk/32/2174_2.png) [@dirk](https://discourse.cakephp.org/u/dirk)\
**Post date:** [April 13, 2021, 12:38pm UTC](https://discourse.cakephp.org/t/cake-4-check-for-button-click-in-controller/9233/6 "2021-04-13T12:38:37Z")

</div>

This is the

> debug($this-\>request-\>getData())

from export button:

> `> 'name' => 'Ranker und Schlanker', 'secondname' => '', 'street' => 'Wiesenst. 42', 'zipcode' => '57612', 'town' => 'Giesenhausen', 'phone' => '564654', 'fax' => '', 'email' => 'info1@giesenhausener.de', 'url' => '', 'vatidnumber' => 'sfds', 'taxnumber' => 'sdfsfd', 'businessregistration' => '', 'country_id' => '1', 'state_id' => '3', 'membership_id' => '1', 'export' => '',`

This is the

> debug($this-\>request-\>getData())

from search button:

> ‘name’ =\> ‘Ranker und Schlanker’, ‘secondname’ =\> ‘’, ‘street’ =\> ‘Wiesenst. 42’, ‘zipcode’ =\> ‘57612’, ‘town’ =\> ‘Giesenhausen’, ‘phone’ =\> ‘564654’, ‘fax’ =\> ‘’, ‘email’ =\> ‘info1@giesenhausener.de’, ‘url’ =\> ‘’, ‘vatidnumber’ =\> ‘sfds’, ‘taxnumber’ =\> ‘sdfsfd’, ‘businessregistration’ =\> ‘’, ‘country\_id’ =\> ‘1’, ‘state\_id’ =\> ‘3’, ‘membership\_id’ =\> ‘1’, ‘search’ =\> ‘’,

---

<div class="post-metadata">

**Author:** ![dirk](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dirk/32/2174_2.png) [@dirk](https://discourse.cakephp.org/u/dirk)\
**Post date:** [April 13, 2021, 12:42pm UTC](https://discourse.cakephp.org/t/cake-4-check-for-button-click-in-controller/9233/7 "2021-04-13T12:42:45Z")

</div>

Other question:  
Wouldn’t it make sense to use postlinks

for example:  
`<?= $this->Form->postLink(__('Export'), ['action' => 'export', $entity->id], ['confirm' =>__('Are you sure you want to export # {0}?', $entity->id)]) ?>`

---

<div class="post-metadata">

**Author:** ![die7fox](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/die7fox/32/1970_2.png) [@die7fox](https://discourse.cakephp.org/u/die7fox)\
**Post date:** [April 13, 2021, 12:49pm UTC](https://discourse.cakephp.org/t/cake-4-check-for-button-click-in-controller/9233/8 "2021-04-13T12:49:50Z")

</div>

I could use a postlink, but I can’t pass an array to it, as far as I know, and I need the full `$query` array for the export to work.

Your previous post did put me on to something, though. In my app, since I’m using a search plugin, I use `$this->request->getQueryParams()` to read the form data instead of `$this->request->getData()`, so that would explain why the latter always comes up null in a debug (I’m spitballing there, but debug of `getQueryParams()` works, whereas debug of `getData()` does not). Neither shows that my button click evaluates to anything, though.

---

<div class="post-metadata">

**Author:** ![dirk](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dirk/32/2174_2.png) [@dirk](https://discourse.cakephp.org/u/dirk)\
**Post date:** [April 13, 2021, 1:03pm UTC](https://discourse.cakephp.org/t/cake-4-check-for-button-click-in-controller/9233/9 "2021-04-13T13:03:11Z")

</div>

Just to clarify:  
If you hit i.e. the “export Button” and then in your controller do

> debug($this-\>request-\>getData('export);

the result will be NULL, because the key “export” has empty value.  
So you have to go this way, which checks, if there is a key named “export”:

```
$export = $this->request->getData('export');
      if (isset($export)) {
        debug($this->request->getData());
      }

```

---

<div class="post-metadata">

**Author:** ![die7fox](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/die7fox/32/1970_2.png) [@die7fox](https://discourse.cakephp.org/u/die7fox)\
**Post date:** [April 13, 2021, 1:07pm UTC](https://discourse.cakephp.org/t/cake-4-check-for-button-click-in-controller/9233/10 "2021-04-13T13:07:48Z")

</div>

Yep, tried that in both my `search()` and `export()` methods, to the letter. Doesn’t work in either case. App just isn’t recognizing any button click as input.

---

<div class="post-metadata">

**Author:** ![dirk](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dirk/32/2174_2.png) [@dirk](https://discourse.cakephp.org/u/dirk)\
**Post date:** [April 13, 2021, 1:33pm UTC](https://discourse.cakephp.org/t/cake-4-check-for-button-click-in-controller/9233/11 "2021-04-13T13:33:04Z")

</div>

One last idea:  
3 Buttons in your Form:

> ```
> <?= $this->Form->button(__('export'),['name'=>'action','value'=>'export']) ?>
> <?= $this->Form->button(__('search'),['name'=>'action','value'=>'search']) ?>
> <?= $this->Form->button(__('reset'),['name'=>'action','value'=>'reset']) ?>
> 
> ```

And in the Controller:

> ```
> $action = $this->request->getData('action');
> debug($action);
> 
> ```

---

<div class="post-metadata">

**Author:** ![Edwin](https://avatars.discourse-cdn.com/v4/letter/e/73ab20/32.png) [@Edwin](https://discourse.cakephp.org/u/Edwin)\
**Post date:** [April 18, 2021, 3:18pm UTC](https://discourse.cakephp.org/t/cake-4-check-for-button-click-in-controller/9233/12 "2021-04-18T15:18:23Z")

</div>

You will have to use a hidden hield and some javascript,

If the export button is clicked fill the hidden field with a value (export), and then submit the form.

Check in the controller the value of the hidden field.
