<p>That's just the way forms work in html. If you don't want to use a POST method, (because that would make the results not URL-addressable at all), I'd recommend using an intermediary Twig template to perform a redirect based on what parameters exist - i.e., set your form action to <code>search/process</code> and in that template write a load of conditional statements to build up a query string to pass into a <code>{% redirect %}</code> tag which loads <code>search/results</code>.</p>
<p>If you don't want two templates, you could instead set your form method to <code>POST</code> and then in your <code>search/results</code> template you could conditionally perform a redirect to the same template appended with your custom query string, and use <code>craft.app.request.method</code> to distinguish between the request types to avoid an inifinte redirect loop.</p>

CC BY-SA 4.0
2020-09-01T19:51:43.297Z
Gotcha. I was thinking there may have been a way to set up the conditionals on the form itself such that if a field was empty or not selected, the field name itself wouldn't be passed into the search query. But I guess that's not possible.
user3092
CC BY-SA 4.0
2020-09-01T20:50:25.753Z
You could only do that with JavaScript - and it's probably not worth the technical debt.
James Smith
CC BY-SA 4.0
2020-09-01T07:15:09.490Z
2020-09-01T07:15:09.490Z
James Smith
<p>Is it possible to have a search form pass (into the search query) the field params of only fields that have been populated/selected? For example, if the following form is submitted, both <code>q</code> and <code>color</code> params are passed in the receiving template to which the form submits.</p>
<pre><code>&lt;form action=&quot;{{ url('search/results') }}&quot;&gt;
    &lt;input type=&quot;search&quot; name=&quot;q&quot;&gt;
    &lt;select name=&quot;color&quot;&gt;
        &lt;option value=&quot;&quot;&gt;Select a Color&lt;/option&gt;
        &lt;option value=&quot;black&quot;&gt;Black&lt;/option&gt;
        &lt;option value=&quot;white&quot;&gt;White&lt;/option&gt;
    &lt;/select&gt;
    &lt;input type=&quot;submit&quot; value=&quot;Go&quot;&gt;
&lt;/form&gt;
</code></pre>
<p>Output URL: <code>http://mydomain.com/people/results?q=&amp;color=</code></p>
<p>I have a search form that includes a lot of fields, so I want to only pass the params that are not empty or have been selected. This way, the URL that's passed is cleaner and less monstrous.</p>
<p>So in the example above, if the color dropdown isn't selected, the output URL would be <code>http://mydomain.com/people/results?q=somequerystring</code></p>

<craft3><search><params><querystring>
Search form - Passing only field params that are not empty or selected
# Search form - Passing only field params that are not empty or selected

(Asked by: username_0 on 2020-08-31)

Is it possible to have a search form pass (into the search query) the field params of only fields that have been populated/selected? For example, if the following form is submitted, both q and color params are passed in the receiving template to which the form submits.

    
    
        Select a Color
        Black
        White
    
    


Output URL: http://mydomain.com/people/results?q=&color=
I have a search form that includes a lot of fields, so I want to only pass the params that are not empty or have been selected. This way, the URL that's passed is cleaner and less monstrous.
So in the example above, if the color dropdown isn't selected, the output URL would be http://mydomain.com/people/results?q=somequerystring

**Tags:** craft3, search, params, querystring


---

## 1 answers

### Answer by username_1 on 2020-09-01. Score: 1 | Accepted answer

That's just the way forms work in html. If you don't want to use a POST method, (because that would make the results not URL-addressable at all), I'd recommend using an intermediary Twig template to perform a redirect based on what parameters exist - i.e., set your form action to `search/process` and in that template write a load of conditional statements to build up a query string to pass into a `{% redirect %}` tag which loads `search/results`.

If you don't want two templates, you could instead set your form method to `POST` and then in your `search/results` template you could conditionally perform a redirect to the same template appended with your custom query string, and use `craft.app.request.method` to distinguish between the request types to avoid an inifinte redirect loop.

> **Comments:**
> **username_0**: Gotcha. I was thinking there may have been a way to set up the conditionals on the form itself such that if a field was empty or not selected, the field name itself wouldn't be passed into the search query. But I guess that's not possible.  
> **username_1**: You could only do that with JavaScript - and it's probably not worth the technical debt.  

---

