Submitting a form with ajax in Wordpress



PHP Snippet 1:

$('#ajax-contact-form').submit(function(e){
    var name = $("#name").val();
    $.ajax({ 
         data: {action: 'contact_form', name:name},
         type: 'post',
         url: ajaxurl,
         success: function(data) {
              console.log(data); //should print out the name since you sent it along

        }
    });

});

PHP Snippet 2:

$('#ajax-contact-form').submit(function(e){

$.ajax({ 
     data: {action: 'contact_form', name:name},
     type: 'post',
     url: ajaxurl,      
     success: function(data) {
          alert(data); 
    }
});

PHP Snippet 3:

wp_register_script('my_script_handle','');
wp_add_inline_script('my_script_handle', "var techy_ajaxurl = '".admin_url( 'admin-ajax.php' )."';"  );
wp_enqueue_script( 'my_script_handle' );

PHP Snippet 4:

$.ajax({ 
     data: {action: 'contact_form', name:name},
     type: 'post',
     url: techy_ajaxurl,      
     success: function(data) {
          alert(data); 
    }
});