Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
728 views
in Technique[技术] by (71.8m points)

cakephp - How to detect which button was used to submit the form?

I can't seem to detect a button press in cakephp4. In cake3 it was easy.

In Form I have:

echo $this->Form->button('View Data', [
    'name' => 'viewdata',
    'type' => 'submit',
    'class' => 'btn btn-primary'
]); 

In Controller I have:

if ($this->request->is('post') ) {
    .... 
    debug($this->request->getDdata());
    if (!empty($this->request->getData('viewdata'))) {

I click the button and I see 'viewdata'='' so it detects the button has been clicked but its made the value = null? and I can't invoke the button press function of viewdata.

https://book.cakephp.org/4/en/views/helpers/form.html#creating-buttons-and-submit-elements

question from:https://stackoverflow.com/questions/65920155/how-to-detect-which-button-was-used-to-submit-the-form

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The value is not null, it's an empty string. So you could for example do a strict check for that exact value, ie:

if ($this->request->getData('viewdata') === '') {
    // view data submit button was used
}

Or just check for the keys existence by comparing against null:

if ($this->request->getData('viewdata') !== null) {
    // view data submit button was used
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...