Ad

Searchfuntion Working With Ajax Calls In Symfony2

- 1 answer

I am coding some basic crud application in Symfony2 where I want to implement some type of search function on a certain page.

The idea is that I want to launch a search query by entering something in an inputfield which is going to fire an ajaxcall (I could not think of anything better). The response of that ajaxcall has to be a kind of popup list with clickable items that is placed into another field on the initial page when clicking an item.

I have two questions: Is there a better approach than ajax and how can I resolve the problem of the 'popup list' thing.

Second: I can make post ajaxcalls in Symfony2 with this kind of code:

var data = 'test';
        $.ajax({
            url: "{{ path('test_oost') }}",
            data: { data: data },
            method: "post",
            success: function(data) {
                //some things here
            }

But I thought it is a bit strange to use post and I wanted to use get.. Apparently this is not working as I can not retrieve my data in the controller..

EDIT: I forgot to post my controller where I am handling the ajax call, here is the code:

    public function testGetAction(Request $request)
{
    $data = $request->request->get('data');
    $em = $this->getDoctrine()->getManager();

    $entity = $em->getRepository('EuropsimProductBundle:SimProfile')->find($data);
    return new Response($entity); }

This is working fine with method: "post", but failing when I try to use "get".

I also read about typeahead and this is really close to what I meant, the thing is I want a custom little popup or something because the ajax is supposed to return an array of objects with multiple attributes that has to be shown and where mulitple items are selectable. You can see it as two steps where you first launch the searchquery which bring you to a kind of popup or something where you can select the desired rows for further use on the page.

Thanks in advance! Hicy

Ad

Answer

You have to use method $request->query:

For GET method:

$data = $request->query->get('data');

For POST method:

$data = $request->request->get('data');
Ad
source: stackoverflow.com
Ad