Show the results of a search on separate page

Hi there,

I followed the Kirby tutorial on building the search, worked like a charm, but now I have a further question:

Question

On my homepage (domain.com/home) I added a search bar. When I enter a keyword it shows me the results on the same page. But what I want to achieve is that whenever I submit the form, it takes the user to a separate search result page with its own template (domain.com/search). How do I pass the query to a different page?

Here is the code so far:

  <form>
    <input type="search" placeholder="Search …" autocomplete="off" name="q" value="<?= html(get('q')) ?>">
    <input type="submit" value="Search">
  </form>

  <ul>
    <?php foreach (page('wiki')->search(get('q'), 'title|text') as $result) : ?>
      <li>
        <a href="<?= $result->url() ?>">
          <?= $result->title() ?>
        </a>
      </li>
    <?php endforeach ?>
  </ul>
  1. Create a search page with a search.txt and a search.php controller/template.
  2. In your form’s action attribute, add <?php echo page('search')->url() ?>.
  3. The logic for handling the request then goes into the search.php controller, and html for displaying the results into the search.php template.
1 Like

This really helped me a lot, thank you!

As this was my first time dealing with search forms, maybe your answer is something to add to the cookbook? Such a small but powerful detail! I am sure there are a lot of newbies like me that would be thankful for this addition.

Definitely! I wasn’t aware that the form didn’t have an action attribute