Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP submitting a form to right action

Tags:

php

cakephp

I have this in add.ctp:

<!-- File: /app/views/posts/add.ctp --> 

<h1>Add Post</h1>
<?php
echo $form->create('Post');
echo $form->input('title');
echo $form->input('body', array('rows' => '3'));
echo $form->end('Save Post');
?>

and this in my controller:

function add(){
    if (!empty($this->data)) {
        if($this->Post->save($this->data)){
            $this->Session->setFlash('Your post has been saved');
            $this->redirect(array('action' => 'index'));
        }
    }
}

My question is how does CakePHP know that when the user hits submit, to send "data" to the function "add" in the controller?

like image 696
benhowdle89 Avatar asked Mar 12 '11 21:03

benhowdle89


2 Answers

By default CakePHP will send the form to the same action that displayed it.

You can change it in the view as follows:

echo $form->create('Post', array('action' => 'whatever'));
like image 58
Andrea Avatar answered Oct 20 '22 00:10

Andrea


or if you want to redirect to another controller as well you can use this

echo $form->create('Post', array('url' => '/controller_name/action_name'));

like image 39
dav Avatar answered Oct 20 '22 01:10

dav