Search in This Blog

Form in CakePHP 3.0

Problem:

How to create Form in CakePHP 3.0

Solution:

To create form in CakePHP 3.0 you need to use $this->Form->create() and along with this also use
echo $this->Form->input(), $this->Form->label(), $this->Form->text(), $this->Form->button() and never forget that every for must close with $this->Form->end(array('')) method.


Example:

Create this view file  "showhardselection.ctp" under "src\Template\HardSelection" folder.
<?php
echo $this->Form->create('UserForm', array('url' => array(
                        'controller' => 'HardSelection',
                        'action' => 'showsubmitdata')));
echo $this->Form->input('email');
echo $this->Form->label('username', 'Your username');
echo $this->Form->text('username');
echo $this->Form->button('Submit');
echo $this->Form->end(array(''));
?>

Create this controller file "HardSelectionController.php" in"src\Controller" folder.
<?php
namespace App\Controller;
use Cake\Core\Configure;
use Cake\Network\Exception\NotFoundException;
use Cake\View\Exception\MissingTemplateException;

class HardSelectionController extends AppController
 {
     public function index() {

    }
    public function showhardselection(){
       
    }
    public function showsubmitdata() {
        if($this->request->is('post')) {
            $email = $this->request->data['email'];
            $username = $this->request->data['username'];
            $this->set('email',$email);
            $this->set('username',$username);           
        }
    }
}

Create this view file  "showsubmitdata.ctp" under "src\Template\HardSelection" folder.
<?php
if(isset($email)) {
    echo 'user email:'.$email;   
}
if(isset($username)){
    echo '<br/>user name:'.$username;   
}
?>

Previous
Next Post »