Created
January 25, 2016 00:30
-
-
Save lucasmlessa/0ae9ebba78349bde2cc4 to your computer and use it in GitHub Desktop.
Consulta de Listas em Ajax com método POST
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| if($_POST['valor'] == '001'){ echo "Águas Lindoya";} | |
| elseif($_POST['valor'] == '003'){echo "Água Bonafont";} | |
| elseif($_POST['valor'] == '004'){echo "Água Cristal";} | |
| elseif($_POST['valor'] == '005'){echo "Águas Perrier";} | |
| else{echo "Nada encontrado =/";} | |
| ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <title>Consulta de Listas em Ajax com método POST</title> | |
| <style type="text/css">#resultado{font-size: 48px;}</style> | |
| <script src="https://code.jquery.com/jquery-1.11.1.min.js"></script> | |
| <script type="text/javascript"> | |
| /* Aguarda a página carregar para executar a função */ | |
| $(document).ready(function(){ | |
| /* Seleciona qualquer elemento dentro do formulário e dispara a função quando o formulario perder o foco */ | |
| $("#formulario > input, textarea, select").blur(function(){ | |
| /* Define a variável dados como uma string com todos os valores do formulário para facilitar o envio */ | |
| var dados = $( '#formulario' ).serialize(); | |
| /* inicia uma chamada em ajax */ | |
| $.ajax({ | |
| /* tipo de método de requisição (GET ou POST) */ | |
| type: "POST", | |
| /* arquivo PHP que vai executar o processamento */ | |
| url: "pos.php", | |
| /* Quais dados serão enviados, neste caso a variável "dados" */ | |
| data: dados, | |
| /* Se o servidor responder corretamente executa uma função */ | |
| success: function(data) | |
| { | |
| /* insere a variável "data"(resposta do servidor) dentro do elemento com o id "resultado" */ | |
| $('#resultado').html(data) | |
| } | |
| }); | |
| /* Desabilita o submit do formulário */ | |
| return false; | |
| }); | |
| }); | |
| </script> | |
| </head> | |
| <body> | |
| <form action="" id="formulario" name="form"><input type="text" name="valor"></form> | |
| <p id="resultado"></p> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment