Here's another way: in case you want to manage that asynchronously, you can create an AJAX call like this(I usually place it in my <theme>/inc
folder, so it's automatically loaded):
add_action( 'wp_ajax_nopriv_<call_name>', '<function name>' );
add_action( 'wp_ajax_<call_name>', '<function name>' );
function <function_name>() {//here you do things with $_POST and return json}
Localize it in your functions.php
file in order to have your ajaxUrl for the request always available in a variable and only for your js file:
wp_localize_script( 'enqueued-js-name', 'varName', array(
'ajaxUrl' => admin_url( 'admin-ajax.php' )
));
And then you can call it like this in your enqueued JS file:
jQuery.ajax({
url: varName.ajaxUrl,
type: 'post',
data: currentDataToSubmit,
success: function(response){
//manage the response
}
});
And that's it! hope it was useful!
If there's something wrong with the first part, let me know, so I can learn too!