<p>I did a little more research on Ajax and found a solution. I updated my Ajax such that the data line looks like:</p>

<pre><code>$.ajax({
    method: 'get',
    url: 'actions/websiteListing/checkStatus',
    data: {filter: data},
    headers: {'Content-Type': 'application/x-www-form-urlencoded'}
})
</code></pre>

<p>Then in my controller, I was able to pass that data by adding $filter to the first line and including that for my 3rd $criteria line:</p>

<pre><code>public function actionCheckStatus($filter)
{
    $criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
    $criteria-&gt;section = ('live');
    $criteria-&gt;hostSite = $filter;
    $criteria-&gt;limit = null;

    $entries = $criteria-&gt;find();

    foreach ($criteria as $entry) {
        echo $entry-&gt;title . '&lt;br&gt;';
    }
    die();
}
</code></pre>

CC BY-SA 3.0
2018-03-29T13:49:25.663Z
2018-03-29T15:29:28.150Z
2018-03-29T15:29:28.150Z
Steve VW
<p>Short story: I need your help sending value from dropdown selection as data to controller so my div updates with the filtered entries based on the selection.</p>

<p>Long story and code: I have a listing of entries in my template as follows:</p>

<pre><code>{% for entry in craft.entries.section('live').find() %}
    &lt;li&gt;&lt;a href="{{ entry.url }}"&gt;{{ entry.title }}&lt;/a&gt;&lt;/li&gt;
{% endfor %}
</code></pre>

<p>I know I can filter those results with a field handle by updating the first line above with:</p>

<pre><code>{% for entry in craft.entries.section('live').hostSite('weDo').find() %}
</code></pre>

<p>However I'd like to filter that listing of entries with a dropdown list that contains the available field handles, ie 'weDo, 'theyDo':</p>

<pre><code>&lt;select class="filter"&gt;
    &lt;option value="all"&gt;all sites&lt;/option&gt;
    &lt;option value="weDo"&gt;sites we host&lt;/option&gt;
    &lt;option value="theyDo"&gt;sites they host&lt;/option&gt;
&lt;/select&gt; 
</code></pre>

<p>I'd like to do this with Ajax and a controller I set up:</p>

<pre><code>**Javascript**
// Add an event listener for filter clicks
$(function () {
    $('body').on('change', 'select.filter', onFilterClick);
});

// Event handler for the filter click
function onFilterClick (e)
{
    e.preventDefault();
    var data = this.value;

    $.ajax({
        method: 'get',
        url: 'actions/websiteListing/checkStatus',
        data: data,
        headers: {'Content-Type': 'application/x-www-form-urlencoded'}
    })
    .success(function(data) {
        $('.livelisting').html(data);
    })
    .error(function(data) {
        console.log('error', data);
    }); 
}
**Controller**
&lt;?php
namespace Craft;

class WebsiteListingController extends BaseController
{
    protected $allowAnonymous = true;

    public function actionCheckStatus()
    {
        $criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
        $criteria-&gt;section('live');
        $criteria-&gt;hostSite = 'weDo';
        $criteria-&gt;limit = null;

        $entries = $criteria-&gt;find();

        foreach ($criteria as $entry) {
            echo $entry-&gt;title . '&lt;br&gt;';
        }
    }
}
</code></pre>

<p>I know the ajax is pretty trashy, but the url to the controller file is working correctly because when I make a selection from the dropdown menu, my div.livelisting is replaced by the single entry available with the field handle of 'weDo'. </p>

<p><strong>Here's where your help is needed!</strong> The 'weDo' value in my controller needs the data sent from Ajax. Thanks for your expertise.</p>

<ajax><filter><controller>
Filter entries by ajax to controller
# Filter entries by ajax to controller

(Asked by: username_0 on 2018-03-28)

Short story: I need your help sending value from dropdown selection as data to controller so my div updates with the filtered entries based on the selection.
Long story and code: I have a listing of entries in my template as follows:
{% for entry in craft.entries.section('live').find() %}
    {{ entry.title }}
{% endfor %}

I know I can filter those results with a field handle by updating the first line above with:
{% for entry in craft.entries.section('live').hostSite('weDo').find() %}

However I'd like to filter that listing of entries with a dropdown list that contains the available field handles, ie 'weDo, 'theyDo':

    all sites
    sites we host
    sites they host
 

I'd like to do this with Ajax and a controller I set up:
**Javascript**
// Add an event listener for filter clicks
$(function () {
    $('body').on('change', 'select.filter', onFilterClick);
});

// Event handler for the filter click
function onFilterClick (e)
{
    e.preventDefault();
    var data = this.value;

    $.ajax({
        method: 'get',
        url: 'actions/websiteListing/checkStatus',
        data: data,
        headers: {'Content-Type': 'application/x-www-form-urlencoded'}
    })
    .success(function(data) {
        $('.livelisting').html(data);
    })
    .error(function(data) {
        console.log('error', data);
    }); 
}
**Controller**
elements->getCriteria(ElementType::Entry);
        $criteria->section('live');
        $criteria->hostSite = 'weDo';
        $criteria->limit = null;

        $entries = $criteria->find();

        foreach ($criteria as $entry) {
            echo $entry->title . '';
        }
    }
}

I know the ajax is pretty trashy, but the url to the controller file is working correctly because when I make a selection from the dropdown menu, my div.livelisting is replaced by the single entry available with the field handle of 'weDo'. 
Here's where your help is needed! The 'weDo' value in my controller needs the data sent from Ajax. Thanks for your expertise.

**Tags:** ajax, filter, controller

---

## 1 answers

### Answer by username_0 on 2018-03-29. Score: 1 | Accepted answer

I did a little more research on Ajax and found a solution. I updated my Ajax such that the data line looks like:
$.ajax({
    method: 'get',
    url: 'actions/websiteListing/checkStatus',
    data: {filter: data},
    headers: {'Content-Type': 'application/x-www-form-urlencoded'}
})

Then in my controller, I was able to pass that data by adding $filter to the first line and including that for my 3rd $criteria line:
public function actionCheckStatus($filter)
{
    $criteria = craft()->elements->getCriteria(ElementType::Entry);
    $criteria->section = ('live');
    $criteria->hostSite = $filter;
    $criteria->limit = null;

    $entries = $criteria->find();

    foreach ($criteria as $entry) {
        echo $entry->title . '';
    }
    die();
}


---

