• Resolved mukape

    (@mukape)


    Hello,

    We’re trying to add a counter field with start/stop field, so while the user is submitting the form he can click on the start, and when he finish he can click on finish, and this time to be calculated and sent with the form submission
    then the form submission will have also the start time in text, end time in text, and the calculated time/period between start and end

    Kindly let me know if this feature is available, or how to do that?

    Regards

    • This topic was modified 10 months, 1 week ago by mukape.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support Dmytro – WPMU DEV Support

    (@wpmudevsupport16)

    Hello @mukape,

    I hope you’re doing good today!

    There’s no such time calculation features in Forminator, so we’re checking with the Second Line Support team, if that’s something that could be done with additional PHP coding.

    We’ll be letting you know, once there’s more info from the SLS techs regarding this. Please note, that the next reply may take longer than our usual response time, depending on the amount and complexity of the tasks currently assigned for the developers.

    Best Regards,
    Dmytro

    Plugin Support Laura – WPMU DEV Support

    (@wpmudevsupport3)

    Hi @mukape,

    Hope this message finds you well.

    Our Second Line Support was able to provide this mu-plugin:

    add_action( 'wp_footer', 'wpmudev_start_form_calculate', 9999 );
    function wpmudev_start_form_calculate() {
        global $post;
        if ( is_a( $post, 'WP_Post' ) && !has_shortcode( $post->post_content, 'forminator_form' ) ) {
            return;
        }
    	?>
    	<script type="text/javascript">
    	jQuery(document).ready(function($){
    		setTimeout(function() {
    			$('.forminator-custom-form').trigger('after.load.forminator');
    		},100);
    
    		$(document).on('after.load.forminator', function( e, form_id ) {
                if ( e.target.id == 'forminator-module-6' ) { // Please change 6 to your form's ID.
                    $(document).on('click', '.forminator-button-next', function(){
                        var date_filled = $('input[name="hidden-1"]').val();
                        if ( ! date_filled ) {
                            var date_start = new Date();
                            var time = date_start.getTime();
                            $('input[name="hidden-1"]').val(time);
                        }
                    });
    
                    $(document).on('click', '#forminator-submit', function(){
                        var date_end = new Date();
                        var time_end = date_end.getTime();
                        $('input[name="hidden-2"]').val(time_end);
                        var diff = time_end - $('input[name="hidden-1"]').val();
                        $('input[name="hidden-3"]').val( millisToMinutesAndSeconds(diff) );
                    });
                }
            });
    
            function millisToMinutesAndSeconds(millis) {
                var minutes = Math.floor(millis / 60000);
                var seconds = ((millis % 60000) / 1000).toFixed(0);
                return minutes + ":" + (seconds < 10 ? '0' : '') + seconds;
            }
        });
        </script>
        <?php
    }
    
    add_filter( 'forminator_prepared_data', 'wpmudev_update_hidden_field_val', 10, 2 );
    function wpmudev_update_hidden_field_val( $prepared_data, $module_object ) {
        $form_ids = array(6); // Please change the form ID.
    	if ( !in_array( $module_object->id, $form_ids ) ) {
    		return $prepared_data;
    	}
        
        foreach ( $prepared_data as $key => $value ) {
            if ( strpos( $key, 'hidden-1' ) !== false ) {
                $prepared_data[$key] = sanitize_text_field( $_POST[$key] );
            }
    
            if ( strpos( $key, 'hidden-2' ) !== false ) {
                $prepared_data[$key] = sanitize_text_field( $_POST[$key] );
            }
    
            if ( strpos( $key, 'hidden-3' ) !== false ) {
                $prepared_data[$key] = sanitize_text_field( $_POST[$key] );
            }
        }
        
    	return $prepared_data;
    }
    
    add_action( 'forminator_custom_form_submit_before_set_fields', 'wpmudev_change_hidden_field_data', 10, 3 );
    function wpmudev_change_hidden_field_data( $entry, $module_id, $field_data_array ) {
        $form_ids = array(6); // Please change the form ID.
    	if ( ! in_array( $module_id, $form_ids ) ) {
    		return;
    	}
        
        foreach ( $field_data_array as $key => $value ) {
            if ( strpos( $value['name'], 'hidden-1' ) !== false ) {
                Forminator_CForm_Front_Action::$info['field_data_array'][$key]['value'] = sanitize_text_field( $_POST[$value['name']] );
            }
    
            if ( strpos( $value['name'], 'hidden-2' ) !== false ) {
                Forminator_CForm_Front_Action::$info['field_data_array'][$key]['value'] = sanitize_text_field( $_POST[$value['name']] );
            }
    
            if ( strpos( $value['name'], 'hidden-3' ) !== false ) {
                Forminator_CForm_Front_Action::$info['field_data_array'][$key]['value'] = sanitize_text_field( $_POST[$value['name']] );
            }
        }
    
    }

    Note that you need to update your form ID on this line (replacing 6):

    if ( e.target.id == 'forminator-module-6' )

    You will find how to install a mu-plugin on this link https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins. The first step is only a Next button (changed text to Start), then add three hidden fields with custom values (start time, end time, total time).

    You will find a test form on this link: https://drive.google.com/file/d/1OyeOeLosAdzDpVb56l54aQ-UJtQAmGKa/view?usp=sharing

    Let us know if you require additional assistance.

    Best regards,
    Laura

    Plugin Support Kris – WPMU DEV Support

    (@wpmudevsupport13)

    Hi @mukape

    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

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Start/Stop Coutner Field / Calculate period’ is closed to new replies.