1

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

5 Answers 5

8

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
}
Sign up to request clarification or add additional context in comments.

Comments

5

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.

1 Comment

I think it would have been good if SO could force people to leave a comment while voting up or down. I received a vote down here but don't know who did it and why?
1

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' // 
    )
);

Comments

0

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>

Comments

-1

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'));

2 Comments

How can i make each button call a different method in the controller?
Why would you want to do that? You can just Save and forward after the save is complete. Either to the same page with the form (Create New) or to the index (Save & Close). See stackoverflow.com/questions/3810124/one-form-two-action. It is hard to accomplish and bad practice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.