Search code examples
phpjqueryformscakephpcakephp-2.3

Multiple Submit buttons in cakephp form


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


Solution

  • 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:

    <?php echo $this->Form->submit('Delete it', array('name'=>'User[formaction]')); ?>
    <?php echo $this->Form->submit('Undelete Selected', array('name'=>'User[formaction]')); ?>
    <?php echo $this->Form->submit('Purge Selected', array('name'=>'User[formaction]')); ?>
    

    where "User" is the model name.