How to Use Ajax in WordPress

jQuery(‘#quick_enquiry_form’).submit(function(e){

var ajaxurl = “<?php echo admin_url(‘admin-ajax.php’); ?>”;
var quickfrm = jQuery(“#quick_enquiry_form”).serialize();

var name = jQuery(‘#name’).val();
var cname = jQuery(‘#cname’).val();
var email = jQuery(‘#email’).val();
var quick = jQuery(‘#quick’).val();
var error = false;

e.preventDefault();

jQuery.ajax({
type: ‘post’,
url: ajaxurl,
data: quickfrm+’&action=quick_enquiry’,
async: false,
beforeSend: function(){
jQuery(‘.btn’).html(‘Sending…’);
},
success: function(data) {
data = data.substring(0,data.length-1);
jQuery(“#quick_enquiry_form”).fadeOut(‘ease’,function(){
jQuery(“#quick_enquiry_form”).html(data);
jQuery(“#quick_enquiry_form”).fadeIn(‘ease’);
});
}
});
})

/************Function.php**************************/

add_action(‘wp_ajax_quick_enquiry’, ‘quick_enquiry’);
add_action(‘wp_ajax_nopriv_quick_enquiry’, ‘quick_enquiry’);

function quick_enquiry()
{

echo $_POST[’email’];

}

 

 

 

Advertisements