2

Well, to be honest i am completely new, not only to Craft but to most PHP stuff as well. I am currently trying to setup a search for my page but i seem to miss a crucial step and can not find any tutorials on what to do.

On my front/ index page i included

<form action="{{ url('search/results') }}">
<input type="search" name="q" placeholder="Search">
<input type="submit" value="Go">
</form>

as stated in the Craft Documentation.

I have also set up a single page for my search results with the code from the Documentation as well:

<h1>Search Results</h1>
{% set query = craft.request.getParam('q') %}
{% set entries = craft.entries.search(query).order('score') %}
{% if entries|length %}
<p>{{ entries|length }} results:</p>
<ul>
    {% for entry in entries %}
        <li><a href="{{ entry.url }}">{{ entry.title }}</a></li>
    {% endfor %}
</ul>
{% else %}
<p>Your search for “{{ query }}” didn’t return any results.</p>
{% endif %}

So, now if i try to search at the index page and hit enter, it just reloads the index page with the search term

site.url/index.php?q=test

So for sure i am missing a step here and am not loading the right page. I guess it should be stated in {{ url('search/results') }}, but i have no idea what to put there.

Would be great if anybody could help me or at least point me to a tutorial that focuses on setting up a site search in Craft CMS.

  • In your code do you close the <form> as you aren't in your example – Justin Dekkers Mar 16 '17 at 9:22
  • sure ;) it just got pushed to the next line and disappeared here, fixed that. thanks – Tobias Dax Mar 16 '17 at 10:16
1

Inside of the <form action="{{ url('search/results') }}"> You define which page should be redirected to when the search form is submitted.

"search/results" would go to the search folder looking for the results.(html / twig).

If you want to redirect to another page you should name the folder (which is "search" now) then the template name. This can be static without creating a "Single" for that page.

If you did create a single that uses that template and you want to go through your single you can just put the slug of the created Single inside your form action.

Example:

You made a folder in craft/templates/ named: search, inside that folder you make a file named: results.

So the file structure would be craft/templates/search/results.twig and the form action would be: <form action="{{ url('search/results') }}">

This way you can just use the preset search template the craft docs offer you. You could also change "results.twig/html" to index.html and then your form action wouldn't need the /results so your action would look like: <form action="{{ url('search') }}">

But I see you get redirect to /index.php/ aswell so I should try rmeoving that by changing your .htaccess file and maybe your general.php.

You can find instructions on how to do that here: https://craftcms.com/support/remove-index.php

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.