The answer is AJAX. Each time the totals are calculated, send the form data to the wp ajax handler.
Step 1: pass your ajax URL from PHP to javascript using wp_localize_script just after you enqueue your script file.
wp_enqueue_script('myScript', 'my/script/path.js');
wp_localize_script('myScript', 'php_params', ['ajaxurl'=>admin_url('admin-ajax.php')]);
Step 2: make the AJAX call in your script.
$.ajax({
type: "POST",
url: php_params.ajaxurl,
data: {
action:'mytag_function_name',
your:'data',
goes:'here'
},
success: function(data){
// do something
},
error: function(errorThrown){
// do something
}
});
Step 3: add the ajax method to your functions.php file
add_action( 'wp_ajax_nopriv_mytag_function_name', 'mytag_function_name' );
add_action( 'wp_ajax_mytag_function_name', 'mytag_function_name' );
function mytag_function_name(){
// do something with your $_POST data
$post = filter_input_array(INPUT_POST);
echo "Here's my result to get returned to Javascript";
die();
}
Learn more about AJAX in the WordPress Codex:
https://codex.wordpress.org/AJAX
Hope this helps 🙂