0

I'm trying to work with ajax in symfony but is some hard for me. I have a form in twig where I get a value that I'm using for search a product in my database:

This is the form in twig:

<form id="myForm" method="post" action="{{ path('my_app_greeting') }}"> <div class="input-group"> <input id="name_id" name="name" type="text" class="form-control" placeholder="Buscar repuesto..." required /> <span class="input-group-btn"> <button id="search-button" class="btn btn-default" type="button">Buscar</button> </span> </div><!-- /input-group --></br> </form>

This is my code using jquery in the same twig template for send the data with ajax:

$(document).ready(function(){
    $("#myForm").submit(function(){
    var value = $("#name_id").val();
    $.ajax({
        type: "POST",
        data: { productValue: value },
        url: "/ventas/test/ajax/"
    })
    .done(function(response){
        template = response;
        $('#output').html(template.content); 
    })
    .fail(function(jqXHR, textStatus, errorThrown){
        alert('Error : ' + errorThrown);
    });
    return false;
    });
});

Later I read about how symfony works with ajax and I thought the best solution for my "problem" is create a new twig template called "ajaxContent.html.twig" for example and do this with the controller:

public function testAction()
{
    $request  = $this->get('request');
    $name     = $request->request->get('productValue');
    $template = $this->renderView('AcmeDemoTwoBundle:Ventas:ajaxContent.html.twig', array('value' => $name));
    $json     = json_encode($template);
    $response = new Response($json, 200);
    $response->headers->set('Content-Type', 'application/json');
    return new Response($response);
}

Of this way later I want to put all the html content inside my original template but I have a problem with json encode. I don't understand very well how it works and how is the better way for receive the data and show it in the twig template. How can I do this? ( I tried to show the data using $('#output').html(template.content); but doesn't work).

Thanks for your help!

3
  • Try renderView instead of render !! Commented Jan 16, 2015 at 4:44
  • Thank you, now I can get the Json but I have a problem when I try to show the information using $('#output').html(template.content); This doesn't show anything but when I try to see the data using an alert this works fine.. Commented Jan 16, 2015 at 5:15
  • You can't put anything but 'html' into jquery html() method. So, if your response is json, then use it as json. If you want to show this json on your page use text() jquery method or wrap this json with html tags. Commented Jan 16, 2015 at 11:33

2 Answers 2

3

Your code in controller seems to be not good.

I mean if you want to return json then what for do you render html (not json) twig template? That's the first.

Then you can get json response by using JsonResponse instead of setting header for standard response.

I don't know what's happening in ajaxContent.html.twig, but I would do something like this:

public function testAction(Request $request)
{
    $name     = $request->get('productValue');
    $response = $this->get('my_response_sercive')->giveMeResponseForTestAction($name);

    return new JsonResponse($response);
}

If you are waiting for html response in you javascript you have to set option dataType: 'html' for your ajax request. Then in testAction you don't have to set 'application/json' header and don't have to json_encode $template variable - but just render your template as you are doing it now. So in this case you could do something like this:

public function testAction(Request $request)
{
    $name     = $request->get('productValue');

    return $this->renderView('AcmeDemoTwoBundle:Ventas:ajaxContent.html.twig', array('value' => $name));
}

After that you can put html into your #output block directly:

$('#output').html(response);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! very useful. I can't vote up because I don't have enough reputation. Sorry.
0

You should use form array first for your problem try this

$template['content']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.