Client Action:
We use JavaScript/jQuery to write the client action script. Here is the simple code which you can use to send the AJAX request with parameters:var my_selected_question = 55;
jQuery.ajax({
url: '/wp-admin/admin-ajax.php',
type: 'POST',
data: { action: 'process_wheel_credit_value',credit_value:my_selected_question},
success: function(data) {
alert(data);
},
error: function() {
alert('There has been an error, please alert us immediately');
}
});
Server Action:
After sending the AJAX request via JavaScript/jQuery, we have to process this request on server to send the response to the client. We use WordPress Actions to handle this AJAX request. WordPress will read the value of action parameter to call the corresponding method on server. In the above case we have used “my_ajax_request”.Now you have to add the below function in the functions.php file of WordPress theme.
// register the ajax action for authenticated users
add_action('wp_ajax_process_wheel_credit_value', 'process_wheel_credit_value');
// register the ajax action for unauthenticated users
add_action('wp_ajax_nopriv_process_wheel_credit_value', 'process_wheel_credit_value');
function process_wheel_credit_value()
{
if(isset($_POST['credit_value']) && !empty($_POST['credit_value'])){
echo $_POST['credit_value'];
//add your logic here
}
}