Two submit buttons on a single page


#1

Is it possible to provide two submit buttons on a single page, one button should update a different table and display the values, the other button is the final submission button that should be clicked after completing the other form fields


#2

Just have a route for each button. But it would make more sense to hide the last button till it’s required.


#3

Thank you.

My requirement is like this,

A secondary table needs to be updated and the data from the secondary table should be displayed on the page after every addition and finally the main table has to be updated and the secondary table should be updated with status as “completed”.
Can two tables updated in a single method inside the controller


#4

Yes they can :slight_smile: If the two tables are related, then you can do something like $this->TableA->TableB->save, otherwise, use the loadModel() method:

$this->loadModel('TableB');
$this->TableB->save()

As for having two submit buttons, the best way would be to give them a name with a unique value:

Template.ctp

$this->Form->submit('One', ['name' => 'submit']);
$this->Form->submit('Two', ['name' => 'submit']);

Controller

if ($this->request->is('post') && $this->request->getData('submit') === 'One') {
//Stuff for one
} elseif ($this->request->is('post') && $this->request->getData('submit') === 'Two') {
//Stuff for two
}

Disclaimer This might not actually work as I typed it out based on memory and have not tested it.


#5

Thank you, that was helpful. data of team are to be added in a form and displayed in the same page. is there any easy way to add multiple user in a page and display the same.

I presently display the data in a main page that contains the form. the form is posted to another method that redirects to this main page on success. However I need to isolate the data based on Session. Could you please help