MainelyDesign.com Blog

Which Submit Button was Clicked in CakePHP? Use Name.

Posted on 03/04/2010 at 12:01 pm by Kevin Wentworth
Viewed 20,121 times | 2 comments

If you want to have multiple submit buttons that make a form do different things there's a simple way to do this.  The name of the submit button that was clicked by the user will be sent (via POST) with all the other POSTed data.  This is very handy if you want to have a cancel button and continue button. In CakePHP the name of the button won't be in $this->data, but it's easy enough to get...

Give the Submit Button a Name

  1. echo $form->submit('Checkout Now', array('name'=>'proceedToCheckout'));

If you don't provide a name for your submit button, when the user clicks the button, the form will simply submit.  If you assign a name (and make sure it's unique) then you can determine which button was clicked by the user.

Find Out Which Button was Clicked

  1. if(isset($this->params['form']['proceedToCheckout'])) {
  2.     //user clicked the submit button named proceedToCheckout... so we'll do that
  3.     $this->redirect(array('action' => 'checkout'));
  4. }

Figuring out which button was clicked by the user is easy...just look in $this->params['form']['buttonName'] where buttonName is what you named your button.  I just check to make sure it's isset() because the value doesn't matter.

Cheers,
-Kevin Wentworth

Bookmark and Share

Tags for Which Submit Button was Clicked in CakePHP? Use Name.

Cakephp | Web Programming | Usage | Example | Forms | Tutorial

Comments for this Posting

Posted by André Vieira

on 20/6/10

That method works well when I'm using simple $form-submit(), but when I try to do it using $ajax->submit(), or $ajax->form() (... ) $form->submit, my $this->params['form'] is always empty.

I'm already saying ajax to use POST.


Thank you

Posted by Reg

on 17/8/11

Cheers mate!
Been searching a while for this.

Sorry, comments are closed for this posting.

Please Email Kevin if you have any questions. Thanks!

Meet Site Avenger - Hosted Content Management System

Powered By: Site Avenger | Site Production: Saco Design