How do I get php variables and pass to a different JS file with ajax?

0

     $(document).ready(function(){
     $("#botao").click(function(){    
     $.ajax({
     url:'../../../consulta.php',
     type: 'POST',
     var senha = document.getElementById('#senha').value;
     data: { senhap: 'senha',
     success: function (data){
     // me disseram que aqui o PHP me retorna alguma coisa, mas como eu vou 
     //tratar o resultado que vier por aqui?
     }
     });
     });
   });

I really do not even know if the above code is totally right, could someone explain both the PHP return and the part of the "date {senh:: password ',}" already thank you!

    
asked by anonymous 03.04.2018 / 14:15

1 answer

2

In your PHP function, you can return something like this:

return new JsonResponse(
                [
                    'success'=>true,
                    'nome' => $nome,
                    'idade' => $idade,
                    'endereco' => $endereco
                ]
            );

and in JS you access this, within the success attribute, such as: data.nome, data.idade , etc. Hope it helps !!

    
03.04.2018 / 18:28