Retrieve data from Symfony action in Ajax

0

Hi, I have a button that triggers a Ajax action that leads to a action in controlador Symfony . In the action I can do select and see the information I want, but I just can not get it in Ajax, in view . Look at my code:

ajax:

  $(document).ready(function() {

        $("#idAlterarResponsavelProcedimento").click(function(){

            var pessoaId = $("#procedimentos_pessoa_origem").attr('name');
            var nome = '<?php echo $nomeFamilia?>';
            alert('nome: '+nome);
            $.ajax({
                cache: false,
                type: 'post',
                dataType: 'json',
                url: '/kwadmin_dev.php/admin/procedimentos/get_responsavel_atual',
                data: { pessoa: pessoaId },

                success: function(data) {
                  alert('success - '+JSON.parse(data)); 
                  // $("#procedimentos_pessoa_origem").html(data.pessoa);
                  $("#procedimentos_pessoa_origem").html('estacio');  
            });
            return false;
        });

My action:

public function executeGet_responsavel_atual(sfWebRequest $request)
{
    $pessoa = $request->getParameter('pessoa');

    $this->familiaId = Doctrine::getTable('familias_pessoas')->createQuery('a')
                                            ->select('a.id_familia')
                                            ->where('a.id_pessoa =' . $pessoa)
                                            ->fetchArray();

    $this->familiaNome = Doctrine::getTable('familias')->createQuery('f')
                                                        ->select('nm_familia')
                                                        ->where('id ='. $this->familiaId[0]["id_familia"])
                                                        ->fetchArray();

    $this->nomeFamilia = $this->familiaNome[0]["nm_familia"];

    //echo $this->nomeFamilia;
    //die;
  //        die($nomeFamilia);


}

and this is my partial that becomes the variable for the view:

   <?php include_partial('form', array('form' => $form,'nomeFamilia' => $nomeFamilia)) ?>

Ai in the source code says: Notice: Undefined variable: familyname

I want to bring to the screen the result I got in the variable $nomeFamilia , but I can not.

    
asked by anonymous 17.11.2015 / 12:07

1 answer

1

Good, for ajax I recommend doing so:

Include the JMS serializer by the composer and install

"require": {
        "jms/serializer-bundle": "^1.1"
    },

Use JMS to serialize the entity to JSON and return the response

public function executeGet_responsavel_atual(sfWebRequest $request)
{
    $pessoa = $request->getParameter('pessoa');

    $this->familiaId = Doctrine::getTable('familias_pessoas')->createQuery('a')
                                            ->select('a.id_familia')
                                            ->where('a.id_pessoa =' . $pessoa)
                                            ->fetchArray();

    $this->familiaNome = Doctrine::getTable('familias')->createQuery('f')
                                                        ->select('nm_familia')
                                                        ->where('id ='. $this->familiaId[0]["id_familia"])
                                                        ->fetchArray();

    $this->nomeFamilia = $this->familiaNome[0]["nm_familia"];

    $serializedEntity = $this->get('serializer')->serialize($this->nomeFamilia, 'json', SerializationContext::create()->setGroups(array('primary')));
    return new Response(serializedEntity);     
}

ajax is correct, only remove the unnecessary:

    $(document).ready(function() {    
        $("#idAlterarResponsavelProcedimento").click(function(){    
            var pessoaId = $("#procedimentos_pessoa_origem").attr('name');
            $.ajax({
                cache: false,
                type: 'post',
                dataType: 'json',
                url: '/kwadmin_dev.php/admin/procedimentos/get_responsavel_atual',
                data: { pessoa: pessoaId },    
                success: function(data) {
                  var pessoa = JSON.parse(data);
                  $("#procedimentos_pessoa_origem").html(pessoa);
            });
            return false;
        });
    
17.11.2015 / 13:08