Advertisement
LucianoCharles2017

form+ajax

Mar 26th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.59 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <meta charset="utf-8">
  5.     <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  6.     <title>Ajax</title>
  7.     <meta name="description" content="">
  8.     <meta name="viewport" content="width=device-width, initial-scale=1">
  9. </head>
  10.  
  11. <body>
  12.     <div id="divRetornoDoEchoJsonEncode"></div>
  13.  
  14.     <form method="POST" style="" action="recebe_form.php" class="ajaxForm">
  15.  
  16.         <input type="text" name="numero" />
  17.  
  18.         <button>Enviar</button>
  19.     </form>
  20.  
  21.     <script src="http://code.jquery.com/jquery-3.3.1.min.js"></script>
  22.     <script>
  23.         let BASE = 'http://localhost/ajuda_grupo/wellingthon/';
  24.     </script>
  25.  
  26.     <script>
  27.         $(function() {
  28.  
  29.             $('.ajaxForm').submit(function(e) {
  30.                 e.preventDefault();
  31.  
  32.                 let form = $(this);
  33.                 let action = form.attr('action');
  34.                 let dados = new FormData($(this)[0]);
  35.  
  36.                 $.ajax({
  37.                     url: BASE + action,
  38.                     data: dados,
  39.                     type: 'POST',
  40.                     dataType: 'json',
  41.                     processData: false,
  42.                     contentType: false,
  43.                     success:function(json){
  44.                       $('#divRetornoDoEchoJsonEncode').html(json);
  45.                     }
  46.  
  47.                 });
  48.             });
  49.         });
  50.     </script>
  51. </body>
  52.  
  53. </html>
  54.  
  55.  
  56.  
  57. arquivo recebe_form.php que receberá os dados enviados do formulario via ajax,
  58.  
  59. <?php
  60.  
  61. $numero = filter_input(INPUT_POST,'numero',FILTER_DEFAULT);
  62.  
  63. echo json_encode($numero);
  64.  
  65. exit;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement