Created
February 3, 2021 02:24
-
-
Save MMazoni/af021885770e970f275319552729e4c5 to your computer and use it in GitHub Desktop.
ajax vanilla js
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
| const form = document.getElementById('form'); | |
| const inputs = { | |
| nome: document.getElementById('nome'), | |
| idade: document.getElementById('idade'), | |
| descricao: document.getElementById('descricao'), | |
| conteudo: document.getElementById('conteudo'), | |
| }; | |
| form.addEventListener('submit', (e) => { | |
| e.preventDefault(); | |
| const request = new XMLHttpRequest(); | |
| request.onload = () => { | |
| let responseObject = null; | |
| try { | |
| responseObject = JSON.parse(request.responseText); | |
| } catch (e) { | |
| console.error('Could not parse JSON!'); | |
| } | |
| if (responseObject) { | |
| console.log(responseObject); | |
| } | |
| }; | |
| inputs.conteudo.value = inputs.descricao.value; | |
| const requestData = `nome=${inputs.nome.value}&idade=${inputs.idade.value}&descricao=${inputs.conteudo.value}`; | |
| request.open('post', 'form.php'); | |
| request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); | |
| request.send(requestData); | |
| }); |
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 | |
| $nome = $_POST["nome"] ?? ''; | |
| $idade = $_POST["idade"] ?? ''; | |
| $descricao = $_POST["descricao"] ?? ''; | |
| header('Content-type: application/json'); | |
| echo json_encode([ | |
| 'ok' => true, | |
| 'nome' => $nome, | |
| 'idade' => $idade, | |
| 'descricao' => $descricao, | |
| ]); |
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="pt-BR"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <title>Document</title> | |
| </head> | |
| <body> | |
| <form id="form" action="forms.php" method="post" enctype="multipart/form"> | |
| <input id="conteudo" type="hidden" name="conteudo"> | |
| <label for="nome">Nome</label> | |
| <input type="text" id="nome" name="nome"> | |
| <br> | |
| <label for="idade">Idade</label> | |
| <input type="number" id="idade" name="idade"> | |
| <br> | |
| <label for="descricao">Descricao</label> | |
| <textarea type="text" id="descricao" name="descricao"></textarea> | |
| <br> | |
| <button type="submit">Enviar</button> | |
| </form> | |
| <script src="app.js"></script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment