How do I pick up php variables and move to a different JS file with ajax?

Asked

Viewed 56 times

0

Next I want to pass a php variable to Javascript only in different files, I have a code template that is this:

     $(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 don’t even know if the above code is totally right, someone could explain both the return of PHP and the part of "data{senhp: 'password',}" I thank you in advance!

  • And the rest of the code? What is the variable PHP who wants to pass?

  • Just a variable $name that is in php, I wanted it to be three: $name, $age and $address, but only name per hour this good size, if only to js send me a Lert would be happy :)

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 attribute Success, as: data.nome, data.idade, etc. I hope it helps!!

Browser other questions tagged

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