WordPress comes with built in support for using AJAX calls from within your plugins or themes functions.php.
Following is the sample example:
Javascript code:
jQuery( document ).on( 'click', '.event-trigger', function() {
var ajax_url = "<?php echo admin_url( 'admin-ajax.php' ); ?>"; //You can define this parameter globally as well.
jQuery.ajax({
url : ajax_url,
type : 'post',
data : {
action : 'youraction',
param1: param1,
param2: param2
},
success : function( response ) {
alert(response)
}
});
})
Write the following code in plugin file or functions.php file in your theme:
add_action( 'wp_ajax_nopriv_youraction', 'your_functionname' ); //This action calls for non-authenticated users as well
add_action( 'wp_ajax_post_youraction', 'your_functionname' ); //This action calls only for authenticated user
function your_functionname() {
//your code goes here
}
Refer Ajax Example for more details.