I have a form in a cakephp view which saves well with a single button, here is the code in the view book_form.ctp
echo $this->Form->create
(
'Book',
array
(
'url' => array
(
'controller' => 'Books',
'action' => 'save_record'
),
'class' => 'span12 the_ajaxform',
'inputDefaults' => array
(
'label' => false,
'error' => false
)
)
);
.
.
// form fields
.
.
$options =array(
'label' => __('Save'),
'class' => 'btn btn-primary',
'id'=>'saveform'
);
echo $this->Form->end($options);
.
.
This works perfect! Now i wanted to add two buttons on that form and this is what i did
$options =array(array(
'label' => __('Save & Close'),
'class' => 'btn btn-primary',
'id'=>'saveform'
),
array(
'label' => __('Save & Create New'),
'class' => 'btn btn-primary',
'id'=>'saveformnew'
)
array(
'label' => __('Cancel'),
'class' => 'btn btn-primary',
'id'=>'formcancel'
));
echo $this->Form->end($options);
But this only brings one button which wont even submit the form,where am i going wrong? and can each button call a different method in the controller? Thanks in advance
If you set the name of the submit button, it will have that as a key in the post data, so you can redirect using that info at the start of your action. e.g.
clicking the first button will give post data like:
Which makes it easy to do something like:
I am not sure whether it is "Technically Correct", HTML4, 5 compatible or not etc. but I have always done it something like this, without any problem so far:
where "User" is the model name.
Usually one form can have just one action
this lmnitation is no longer true in HTML5 where you can set the form action for every button
so: the following code works only for HTML5 browsers
Try using the FormHelper's button function to create the submit button and the other buttons and just call end after that without any options. This will output the buttons and end your form for you.
See: FormHelper::button
e.g.: