0

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

user1870982
  • 9
  • 1
  • 2
  • Not sure whether or not this will solve your problem, but the controller name should be capitalized in your URL parameter: `['url' => ['controller'=>'Courses','action' => 'search']]` – Greg Schmidt Jul 18 '18 at 14:29
  • since the view and the action are the same there is no need to set the url. Anyway your code should work, unless you have some component doing something in the background – arilia Jul 19 '18 at 06:40

1 Answers1

0

try this:

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