I would suggest to simply make an ajax post.
It would be a lot easier to execute whatever function you want straight away depending on the data you send and it passes through admin-ajax either way so you only have to sanitize your fields to be safe.
For example,
form
<form id="myForm" action="" method="post">
<!-- hidden inputs for whatever choice
<input type="submit" name="buttonName" id="buttonName" class="" value="Submit">
</form>
Ajax
$("#myForm").submit(function () {
$.ajax({
type: 'POST',
url: "<?php echo esc_js(admin_url('admin-ajax.php')); ?>",
crossDomain: true,
dataType: 'html',
data: {
"action": "functionName"
},
success: function (response) {
// do something on success if you want
return false;
}
});
return false;
});
function
function functionName()
{
// do whatever here
}
add_action('wp_ajax_nopriv_functionName', 'functionName');
add_action('wp_ajax_functionName', 'functionName');
That’s how I do it at least until now without having any issues.
Hope this helps.
— Edit —-
By the time I finished writing my post you’ve already found your solution but I’m leaving it here for future reference if anybody wants it.
——-
Best regards,
Konstantinos