Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-26T16:03:37.527Z
Answers.LastActivityDate: 2017-04-26T16:21:18.930Z
Answers.LastEditDate: 2017-04-26T16:21:18.930Z
Answers.UserDisplayName: JxM
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-22T02:29:05.160Z
Answers.Text: 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.
Answers.UserDisplayName: Patrick Nesbitt
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-22T02:10:41.207Z
Answers.LastActivityDate: 2014-08-22T02:10:41.207Z
Answers.UserDisplayName: Brad Bell
Body: <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>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-08-22T01:59:39.390Z
Comments.Text: 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?
Comments.UserDisplayName: Brad Bell
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-08-22T02:03:23.013Z
Comments.Text: Well, the form is outside an entries loop so I thought this is the only way to access that data...?
Comments.UserDisplayName: Patrick Nesbitt
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-08-22T02:11:00.900Z
Comments.Text: Yeah, just curious.  Hope the answer helps.
Comments.UserDisplayName: Brad Bell
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-08-26T08:02:01.560Z
Comments.Text: 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?
Comments.UserDisplayName: Johannes Lamers
Tags: <search>
Title: Fetching entries from an advanced search form on the front end
ThreadText: # 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](http://buildwithcraft.com/docs/checkboxes-fields).
  
  This is what the checkbox part of the form looks like:
  
  ```
  {# Checkboxes Based on a Checkbox Field #}
  <label class="heading" for="features">Features</label>
  
  {% for option in craft.fields.getFieldById(15).settings.options %}
      {% set checked = false %}
  
      {% if option.selected %}
          {% set checked = true %}
      {% endif %}
  
      {% if not checked %}
          <input type="hidden" name="fields[features][]" value="">
      {% endif %}
  
      <label>
          <input type="checkbox" name="fields[features][]" value="{{ option.value }}" {% if checked %} checked="checked"{% endif %}>{{ option.label }}
      </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 %}
  <li>
  <label class="form__label" for="{{ option.value }}"><input type="checkbox" name="services[]" id="{{ option.value }}" {% if services is defined and option.value in services %}checked{% endif %} value="{{ option.value }}" >{{ option.label }}</label>
  </li>
  {% 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](http://buildwithcraft.com/classreference/models/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](http://buildwithcraft.com/classreference/fieldtypes/CheckboxesFieldType) (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.  
  
  ---
  
  