sending a text data between template to controller in cakephp 3

160 Views Asked by At

Definition :

I want to send text data from the template (ctp file) to the controller but it is not working.

What I DO Until now :

I have this controller courses and it has function called search as following :

/src/Controller/CoursesController :

    public function search()
     {

    $search = $this->request->getData('keyword');

    debug($search);die;

...

The /src/Template/search:

 <?= $this->Form->create(null, ['url' => ['controller'=>'courses','action' => 'search']]) ?>

 <?= $this->Form->control('keyword', ['label' => false, 'type'=>'text','class'=>'form-control', 'placeholder' => __('Search for...')]); ?>

 <?= $this->Form->button(__('Go'), ['class' => 'btn btn-default', 'type' => 'submit']) ?>


   <?= $this->Form->end() ?>

Despite my attempts to get the text data from the form and print it using debug but unfortunately I've got empty array

1

There are 1 best solutions below

0
mbenjemaa On

try this:

public function search()
{
    $data = $this->request->data;
    if($this->request->is(['patch', 'post', 'put', 'get'])) 
    {
        $search = $this->request->data('keyword');
        debug($search);   
    }
}