AJAX Filter function in Symfony

I created a Symfony Application in which a User can fill out Polls and the Results are saved in the database.
In every Poll the user has to specify in which country he lives, how old he is and some other general Information about himself and then the different other questions.

I have another page which shows some statistics for the poll Results which are calculated in the showAction in the Controller.

Now i want to give the user the ability to choose some filters on top of the page to show the results only from users in a given country or a specific age. Here you can see the input field for the country Filter:

<select id="countryFilter" class="form-control" name="country" data-toggle="country" onchange="filterStats('{{ app.request.getLocale() }}')">

If the user changes the field for example to "Germany" The filterStats-function gets called which should make an ajax-call:

function filterStats(locale){
    var country = document.getElementById("countryFilter").value;
    var state = document.getElementById("stateFilter").value;
    var age = document.getElementById("ageFilter").value;

    var dict = {}
    if (country != ''){dict["country"] = country;}
    if (state != ''){dict["state"] = state;}
    if (age != ''){dict["age"] = age;} 

    $.ajax({
        url: '/'+ locale +'/campaignstatsajax',
        type: 'POST',
        dataType: 'json',
        async: true,

        success: function (){
            window.alert("success")
        },
        error: function (xhr, textStatus, errorThrown){
            window.alert("xhr: " + xhr + "ntextStatus: " + textStatus + "nerrorThrown: " + errorThrown);
        }
    });
}

Here is the route i spacified in routes.yml:

grappt_poll_statisticAjaxfilter:
  path: /{_locale}/campaignstatsajax
  defaults: { _controller: GrapptPollBundle:Statistic:ajaxfilter}

And here is the php-function that should calculates the statistsics with the filters
(I wanted to only show the user-IDs of every user that filled out the campaign for the start and complete the function when i am able to do this for now):

public function ajaxfilterAction(Request $request){

    $campaign = $this->getDoctrine()->getRepository(Campaign::class)->find(1);
    $oneFieldsPollResults = $this->getDoctrine()->getRepository(PollResult::class)->findBy(['field' => $campaign->getOneFieldId()]);
    $userIDs = [];
    foreach ($oneFieldsPollResults as $pollResult) {
        array_push($userIDs, $pollResult->getUser()->getId());
    }
    return new JsonResponse($userIDs);
}

When the filterStats Method gets called i always get an ajax-Error.
First i’d like to know how i get the PHP-function to be called.
How do i pass the dictionary with the Filters to the php-Method?
How do i show the user_ids in my view?

I hope the question is understandable. This is the first time i’m doing an ajax-method in Symfony and i struggle really hard with it.. i hope someone can help me.

Thank you very much for every answer

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *