<p>I came across a similar issue today where I was creating a search form that included checkboxes. I did it as follows.</p>

<p>First, I checked if the URL had the query string I was looking for and joined the query string since it's an array. I used <code>OR</code> because I want it to search for any of the checked options.</p>

<p><code>{% if craft.request.getParam('services') %}
    {% set services = craft.request.getParam('services') %}
    {% set services = services|join(' OR ') %}
{% endif %}
</code></p>

<p>The query string in <code>getParam('services')</code> is the same as the <code>name</code> field of the checkbox.</p>

<p>Then I listed out my options </p>

<p><code>{% for option in craft.fields.getFieldByHandle('memberServices').settings.options %}
&lt;li&gt;
    &lt;label class="form__label" for="{{ option.value }}"&gt;&lt;input type="checkbox" name="services[]" id="{{ option.value }}" {% if services is defined and option.value in services %}checked{% endif %} value="{{ option.value }}" &gt;{{ option.label }}&lt;/label&gt;
&lt;/li&gt;
{% endfor %}
</code></p>

<p>The conditional that gives the checkbox the checked attribute is </p>

<p><code>{% if services is defined and option.value in services %}checked{% endif %}
</code></p>

<p>That is, if services is defined (is in the URL) and the option value is contained within the services variable, apply the checked attribute.</p>

<p>It also works with multiple checkboxes, IE the URL <code>search?q=&amp;services%5B%5D=counselling&amp;services%5B%5D=supervision&amp;sessions=individual</code> will check the "Counselling" and "Supervision" "services" checkboxes, and the "Individual" "sessions" checkbox.</p>

CC BY-SA 3.0
2017-04-26T16:03:37.527Z
2017-04-26T16:21:18.930Z
2017-04-26T16:21:18.930Z
JxM
<p><code>craft.fields.getFieldById()</code> will return a <a href="http://buildwithcraft.com/classreference/models/FieldModel" rel="nofollow">FieldModel</a> and calling settings on that is going to return the actual field settings.</p>

<p>I believe you'll want to get something like this to get the actual <a href="http://buildwithcraft.com/classreference/fieldtypes/CheckboxesFieldType" rel="nofollow">Checkboxes field type</a> (completely untested):</p>

<pre><code>{% set checkboxFieldType = craft.fields.getFieldById(15).getFieldType() %}

{% for option in checkboxFieldType.options %}
    ....
{% endfor %}
</code></pre>

CC BY-SA 3.0
2014-08-22T02:29:05.160Z
No luck, it throws the same error. Incidentally, using craft.fields.getFieldById() for Dropdown fields submits the form fine and returns the correct entries in the search template.
Patrick Nesbitt
CC BY-SA 3.0
2014-08-22T02:10:41.207Z
2014-08-22T02:10:41.207Z
Brad Bell
<p>I'm trying to fetch entries from a channel via a front-end form featuring multiple filters - several dropdown selects and one checkbox field - kind of like an advanced search feature. </p>

<p>The form works OK when using the select dropdown fields but for some reason I can't get the checkbox input in the form to work. I have followed the <a href="http://buildwithcraft.com/docs/checkboxes-fields" rel="nofollow">example in the docs here</a>.</p>

<p>This is what the checkbox part of the form looks like:</p>

<pre><code>{# Checkboxes Based on a Checkbox Field #}
&lt;label class="heading" for="features"&gt;Features&lt;/label&gt;

{% for option in craft.fields.getFieldById(15).settings.options %}
    {% set checked = false %}

    {% if option.selected %}
        {% set checked = true %}
    {% endif %}

    {% if not checked %}
        &lt;input type="hidden" name="fields[features][]" value=""&gt;
    {% endif %}

    &lt;label&gt;
        &lt;input type="checkbox" name="fields[features][]" value="{{ option.value }}" {% if checked %} checked="checked"{% endif %}&gt;{{ option.label }}
    &lt;/label&gt;
{% endfor %}
</code></pre>

<p>This throws an error in the template:</p>

<blockquote>
  <p>Key "selected" for array with keys "label, value, default" does not exist</p>
</blockquote>

<p>On the line above:</p>

<pre><code>{% if option.selected %}
</code></pre>

<p>Further, on my search results template, I'm using <code>getParam</code> to pull the values from the query string like so:</p>

<pre><code>{% set location = craft.request.getParam('location') %}
{% set realEstateType = craft.request.getParam('type') %}
{% set bedrooms = craft.request.getParam('bedrooms') %}
{% set priceRange = craft.request.getParam('price-range') %}
</code></pre>

<p>and then</p>

<pre><code>{% set results = craft.entries.search().location(location).realEstateType(realEstateType).bedrooms(bedrooms).priceRange(priceRange) %}
</code></pre>

<p>This works OK without the offending Checkbox field. I suspect because there are possibly multiple values submitted for a checkbox field that it may not work - should I use search instead of getParam?</p>

<p>Sorry for the long-winded question, any help would be appreciated.</p>

CC BY-SA 3.0
2014-08-22T01:59:39.390Z
Any reason you're going through the field service (craft.fields.getFieldById(15)) to get to the checkbox data and not through the entry that the checkbox field is attached to?
Brad Bell
CC BY-SA 3.0
2014-08-22T02:03:23.013Z
Well, the form is outside an entries loop so I thought this is the only way to access that data...?
Patrick Nesbitt
CC BY-SA 3.0
2014-08-22T02:11:00.900Z
Yeah, just curious.  Hope the answer helps.
Brad Bell
CC BY-SA 3.0
2014-08-26T08:02:01.560Z
Can you share me your template file? I'm working on a same kind of page and can't figure out how to do it. 
Do you use categories in your filter?
Johannes Lamers
<search>
Fetching entries from an advanced search form on the front end
# Fetching entries from an advanced search form on the front end

(Asked by: username_0 on 2014-08-21)

I'm trying to fetch entries from a channel via a front-end form featuring multiple filters - several dropdown selects and one checkbox field - kind of like an advanced search feature. 
The form works OK when using the select dropdown fields but for some reason I can't get the checkbox input in the form to work. I have followed the example in the docs here.
This is what the checkbox part of the form looks like:
{# Checkboxes Based on a Checkbox Field #}
Features

{% for option in craft.fields.getFieldById(15).settings.options %}
    {% set checked = false %}

    {% if option.selected %}
        {% set checked = true %}
    {% endif %}

    {% if not checked %}
        
    {% endif %}

    
        {{ option.label }}
    
{% endfor %}

This throws an error in the template:

Key "selected" for array with keys "label, value, default" does not exist

On the line above:
{% if option.selected %}

Further, on my search results template, I'm using getParam to pull the values from the query string like so:
{% set location = craft.request.getParam('location') %}
{% set realEstateType = craft.request.getParam('type') %}
{% set bedrooms = craft.request.getParam('bedrooms') %}
{% set priceRange = craft.request.getParam('price-range') %}

and then
{% set results = craft.entries.search().location(location).realEstateType(realEstateType).bedrooms(bedrooms).priceRange(priceRange) %}

This works OK without the offending Checkbox field. I suspect because there are possibly multiple values submitted for a checkbox field that it may not work - should I use search instead of getParam?
Sorry for the long-winded question, any help would be appreciated.

**Tags:** search
> **Comments:**
> **username_1**: Any reason you're going through the field service (craft.fields.getFieldById(15)) to get to the checkbox data and not through the entry that the checkbox field is attached to?  
> **username_0**: Well, the form is outside an entries loop so I thought this is the only way to access that data...?  
> **username_1**: Yeah, just curious.  Hope the answer helps.  
> **username_2**: Can you share me your template file? I'm working on a same kind of page and can't figure out how to do it.  Do you use categories in your filter?  

---

## 2 answers

### Answer by username_3 on 2017-04-26. Score: 3

I came across a similar issue today where I was creating a search form that included checkboxes. I did it as follows.

First, I checked if the URL had the query string I was looking for and joined the query string since it's an array. I used `OR` because I want it to search for any of the checked options.

`{% if craft.request.getParam('services') %}
{% set services = craft.request.getParam('services') %}
{% set services = services|join(' OR ') %}
{% endif %}`

The query string in `getParam('services')` is the same as the `name` field of the checkbox.

Then I listed out my options

`{% for option in craft.fields.getFieldByHandle('memberServices').settings.options %}- {{ option.label }}
{% endfor %}`

The conditional that gives the checkbox the checked attribute is

`{% if services is defined and option.value in services %}checked{% endif %}`

That is, if services is defined (is in the URL) and the option value is contained within the services variable, apply the checked attribute.

It also works with multiple checkboxes, IE the URL `search?q=&services%5B%5D=counselling&services%5B%5D=supervision&sessions=individual` will check the "Counselling" and "Supervision" "services" checkboxes, and the "Individual" "sessions" checkbox.


---

### Answer by username_1 on 2014-08-22. Score: 1

craft.fields.getFieldById() will return a FieldModel and calling settings on that is going to return the actual field settings.
I believe you'll want to get something like this to get the actual Checkboxes field type (completely untested):
{% set checkboxFieldType = craft.fields.getFieldById(15).getFieldType() %}

{% for option in checkboxFieldType.options %}
    ....
{% endfor %}

> **Comments:**
> **username_0**: No luck, it throws the same error. Incidentally, using craft.fields.getFieldById() for Dropdown fields submits the form fine and returns the correct entries in the search template.  

---

