Advertisement
adaoduque

Chamando uma função PHP via AJAX

Jul 11th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $( 'form' ).submit( function ( e ) {
  2.    
  3.     //Obtem os dados do form já no formato padrão para envio
  4.     var data  =   $( this ).serialize();
  5.  
  6.     //Envia os dados
  7.     enviarform( data );
  8.    
  9.     //Previne a função padrão do form
  10.     e.preventDefault();
  11.  
  12.     //Previne o redirecionamento do form
  13.     return false;
  14.  
  15. });
  16.  
  17.  
  18.  
  19. var enviarform  =   function (data) {
  20.  
  21.     $.ajax({    
  22.  
  23.         type: 'POST', //Tipo de envio (GET, POST)
  24.  
  25.         url: 'http://localhost/arquivo.php', //URL para o arquivo (URL completa e não PATH)
  26.  
  27.         data: data, //Dados a serem enviados. Formato: key=value. (nome='adao')
  28.  
  29.         dataType: 'json', //json, html, text
  30.        
  31.         success: function (data) {  
  32.            
  33.             //Se houver sucesso
  34.             alert( 'Consegui enviar os dados' );
  35.  
  36.         },error: function(request, status, error){
  37.            
  38.             //Se houver erro no envio
  39.             alert(request.responseText);
  40.        
  41.         }
  42.  
  43.     });
  44.  
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement