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:


On my homepage ( 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 ( How do I pass the query to a different page?

Here is the code so far:

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

    <?php foreach (page('wiki')->search(get('q'), 'title|text') as $result) : ?>
        <a href="<?= $result->url() ?>">
          <?= $result->title() ?>
    <?php endforeach ?>
  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