• Resolved pstidsen

    (@pstidsen)


    Hi there,

    I have a form with a simple calculation field (sum of some other fields), and a currency field. I use the value of the currency field on submission in the forminator_form_after_save_entry and forminator_form_after_handle_submit hook to create a payment link.

    My issue is that the value from the calculation field is not visible in the $_POST variable on submission (in opposition to the currency value, which is visible). Therefore, I was thinking about to copy the value from the calculation field to the currency field, when there are changes in the form. I have tried using this code:

    jQuery(document).ready(function($){
    jQuery('[name=currency-1]').prop('readonly', true);
    jQuery('#forminator-module-2420').on('change', function(event){
    var calculation_val = jQuery('[name=calculation-2]').val();
    jQuery('[name=currency-1]').val(Number(calculation_val));
    });
    });

    However, I am also using the awesome repeater field, and the input elements in new field groups is not triggering the change event bound to the form element. Therefore, the code above only works for fields that were present on page load.

    Is there a way to use the calculation field directly in the entry submission, or would it be better to copy the value from the calculation field to the currency field? If so, how should I do?

    Best Regards,
    Peter B. Stidsen

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    Hi @pstidsen

    I hope you’re well today!

    If you are using “forminator_form_after_save_entry” hook, you can get calculation field value in a different way. This hook is called after entry data (that includes calculation) is already saved into DB so instead of reading POST data (which would also require some additional sanitization/validation in order to be safe) you can simply fetch the entry from the DB.

    To do that, first you’d need to know the entry ID and this can be done by “injecting” it into POST data using different hook. Working example:

    add_action( 'forminator_custom_form_submit_before_set_fields', 'forminator_customfield_entry_id', 20, 3 );
    function forminator_customfield_entry_id( $entry, $form_id, $field_data_array ) {
    
    	$_POST['_forminator_cform_entry_id'] = $entry->entry_id;
    
    }

    At this point in POST in “_forminator_cform_entry_id” you have the ID of that submission. Example use in the “forminator_form_after_save_entry” hook:

    add_action( 'forminator_form_after_save_entry', 'my_save_form_entry', 11, 2 );
    function my_save_form_entry( $form_id, $response ) { 
    
    	if ( $response && is_array( $response ) ) {
    		if ( $response['success'] ) {
    			
    			    // get entry ID
    				$entry_id = $_POST['_forminator_cform_entry_id'];
    				// fetch entry data	
    				$entry =  Forminator_API::get_entry( $form_id, $entry_id );
    				
    		}
    	}
    }

    And now in the “$entry” array you have a full submission data, including calculation field.

    Kind regards,
    Adam

    Plugin Support Kris – WPMU DEV Support

    (@wpmudevsupport13)

    Hi @pstidsen

    We haven’t heard from you in a while, I’ll go and mark this thread as resolved. If you have any additional questions or require further help, please let us know!

    Kind Regards,
    Kris

    Thread Starter pstidsen

    (@pstidsen)

    Hi Kris and Adam,

    Sorry for my late reply. The code you provided fitted my needs perfectly. Thanks!

    Best Regards,
    Peter

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.