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?
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'));
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'));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With