Advertisement
MrPauloeN

Prosty formularz AJAX do WordPressa | Przykład szkielet

Aug 19th, 2017
521
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.98 KB | None | 0 0
  1. //Ten kod należy umieścić w pliku functions.php
  2.  
  3.     add_action('wp_ajax_formularz', '_formularz');
  4.     add_action('wp_ajax_nopriv_formularz', '_formularz');
  5.    
  6.    
  7. function _formularz(){
  8.  
  9.   try {
  10.     if ( empty($_POST['name']) || empty($_POST['email']) || empty($_POST['message']) ) {
  11.       throw new Exception('Puste pola.');
  12.     }
  13.     if ( !is_email($_POST['email']) ) {
  14.       throw new Exception('Niepoprawny email');
  15.     }
  16.    
  17.     $name = $_POST['name'];
  18.     $email = $_POST['email'];
  19.     $message = $_POST['message'];
  20.  
  21.     $headers = 'From: WP Solucje <dev@wpsolucje.pl>';
  22.     $send_to = $email;
  23.     $subject = "Test formularza AJAX : " . $name;
  24.     $message = "Wiadomość od ".$name.": \n\n ". $message . " \n\n Odpowiedz do: " . $email;
  25.  
  26.     if ( wp_mail( $email, $subject, $message, $headers ) ) {
  27.       echo json_encode( 'Sent' );
  28.       exit;
  29.     } else {
  30.       throw new Exception( 'Error sending, Sprawdź AJAX.' );
  31.     }
  32.   } catch ( Exception $e ) {
  33.     echo json_encode( array( 'status' => 'error', 'message' => $e->getMessage() ) );
  34.     exit;
  35.   }
  36.     wp_die();
  37. }
  38.  
  39. // Dodanie zmiennej globalnej do JS:
  40.  
  41.     //formularz kontaktowy
  42.    
  43.     wp_register_script ( 'contact_form', get_template_directory_uri() . '/inc/contakt_form.js', array('jquery'), '1.1.3', true );
  44.     wp_localize_script( 'contact_form', 'ajaxurl', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )));
  45.     wp_enqueue_script ( 'contact_form' );
  46.  
  47. // Plik JS obsługujący formularz:
  48.  
  49. jQuery(document).ready(function ($) {
  50.  
  51.  
  52. // 1. Pierwszys sposob:
  53.  
  54.     // jQuery(document).on( 'submit', '#forma', function(e) {
  55.         // e.preventDefault();
  56.  
  57.         // var data = {
  58.  
  59.             // action: 'formularz', //calls wp_ajax_nopriv_ajaxlogin
  60.             // name: $('form#forma #name').val(),
  61.             // email: $('form#forma #email').val(),
  62.             // message: $('form#forma #message').val(),
  63.  
  64.     // };  
  65.  
  66.         // // We can also pass the url value separately from ajaxurl for front end AJAX implementations
  67.         // $.post( ajaxurl.ajaxurl, data, function( response )   {
  68.             // alert('Odpowiedź z serwera ' + response );
  69.         // });
  70.  
  71.     // });
  72. // .1
  73.  
  74.  
  75.  
  76. // 2. Drugi sposób
  77.  
  78.     $('form#forma').on('submit', function(e){
  79.        
  80.         e.preventDefault();
  81.         var $this = $(this); // Cache this
  82.         $.ajax({
  83.             type: 'POST',
  84.             dataType: 'json',
  85.             url: ajaxurl.ajaxurl,
  86.             data: {
  87.                 action: 'formularz', //calls wp_ajax_nopriv_$your_action
  88.                 name: $('form#forma #name').val(),
  89.                 email: $('form#forma #email').val(),
  90.                 message: $('form#forma #message').val(),
  91.             },
  92.  
  93.             success: function( response ){
  94.                 alert ( response );
  95.             },
  96.            
  97.             error: function ( response ){
  98.                 alert ( response );
  99.             }      
  100.         });    
  101.     });
  102. // .2
  103.    
  104.    
  105. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement