Recover Symfony action data in Ajax

Asked

Viewed 136 times

0

Hi, I have a button that triggers an action Ajax that leads to a action in the controlador Symfony. In the action I can do the select and see the information I want, but I just can’t get it from Ajax, at 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 that’s my partial who passes the variable to the view:

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

Ai in source code says: Notice: Undefined variable: nameFamily

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

1 answer

1

good, by ajax recommend doing so:

include the JMS serialize by the Command and install

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

use JMS to serialize to JSON the entity and return the answer

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 neediness:

    $(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;
        });

Browser other questions tagged

You are not signed in. Login or sign up in order to post.