0

I am implementing a search functionality like this:

index.ctp

<div class="Search">
        <?php
        // The base url is the url where we'll pass the filter parameters
        $base_url = array('controller' => 'ExpiringServices', 'action' => 'index');
        echo $this->Form->create("Filter",array('url' => $base_url, 'class' => 'filter'));
        // Add a basic search 
        echo $this->Form->input("search", array('label' => false, 'placeholder' => "Name or surname..."));

        echo $this->Form->submit("Refresh");

        echo $this->Form->end();
        ?>
</div>

ExpiringServicesController.php

$searchInput = $this->request->data['search']; //line 27

But while search works as expected I receive this error:

Notice (8): Undefined index: search [APP/Controller\ExpiringServicesController.php, line 27]

If I use debug($searchInput) I can see that it contains the search input text. But if I use if (isset($_FILES[$this->request->data['search']])) it won't go inside the if statement, like its not set.

How I can resolve this?

netdev
  • 496
  • 1
  • 5
  • 22
  • Where is `if (isset($_FILES[$this->request->data['search']]))` coming from? The $_FILES part? This only contains uploaded files, if you don't have any (especially with the searched term name) then of course it won't be set? As FrankerZ said, use getData to prevent these notices. – Frank Oct 17 '18 at 13:30

1 Answers1

2

You should use getData() instead:

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

This will prevent the undefined index error, and straight from the docs:

Any keys that do not exist will return null

$foo = $this->request->getData('Value.that.does.not.exist');
// $foo == null
Community
  • 1
  • 1
Blue
  • 22,608
  • 7
  • 62
  • 92