Question

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

Was it helpful?

Solution 2

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.

OTHER TIPS

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.

<?php echo $this->Form->submit('btn1value', array('name'=>'btn1'))?>
<?php echo $this->Form->submit('btn2balue', array('name'=>'btn2'))?>

clicking the first button will give post data like:

array(
    [btn1] => btn1value
    [YourModel] => array(...)
)

Which makes it easy to do something like:

if (isset($this->request->data['btn1'])) {
    // btn1 was clicked
} else if (isset($this->request->data['btn2'])) {
    // btn2 was clicked
}

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

echo $this->Form->button(
    'Your Action Description Here', 
    array(
        'type' => 'submit', 
        'formaction' => 'yourActionHere' // 
    )
);

Try this, This is easy to do.

<div class="submit">
 <?php echo $this->Form->submit(__('Submit', true), array('name' => 'ok', 'div' => false)); ?>
 <a href="link_to_go"><?php   echo $this->Form->button('Cancel', array('type' => 'button'));?></a>

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

echo $this->Form->button('Save & Close', array('type' => 'submit'));
echo $this->Form->button('Save & Create New', array('type' => 'button'));
echo $this->Form->button('Cancel', array('type' => 'reset'));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top