• Resolved killian6341

    (@killian6341)


    Hello,

    As said in the title, when I put a hidden field, then I update it with JS. In my case it’s to retrieve the user’s browser language. I check with the element inspector, the value has been changed. But when I send the form and look the result, the default value I set for the hidden field is sent and not the modified one.

    Is there any way to retrieve the browser language?

    I would like to ask another question, for ? submission time ?, the time is always on GMT+0000 , is there any way to change the time zone to a precise one (in my case eastern time)?

    Thanks a lot for the help!

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Support Kris – WPMU DEV Support

    (@wpmudevsupport13)

    Hi @killian6341

    I hope you are doing well today.

    Could you let us know what JS code you use to change that value?

    Also, what is the Default Value you use in that hidden field? So that we could be on the same page when that value is changed by JS.

    When it comes to submission time it depend from your timezone settings in WordPress. You can change that in WP Dashboard -> Settings -> General -> Timezone.

    Kind Regards,
    Kris

    Thread Starter killian6341

    (@killian6341)

    Hello, thank you very much for your quick reply.

    The string default value is “undefined”
    Here’s the JS code I use :

    const element = document.querySelectorAll('input[name="hidden-2"]');
    elements.forEach(element => {
        element.value = navigator.language || navigator.userLanguage;
    });
    Plugin Support Patrick – WPMU DEV Support

    (@wpmudevsupport12)

    Hi @killian6341

    Sorry for the delay.

    For frontend updates, you can use something like:

    jQuery('input[name="hidden-1"]').val(navigator.language || navigator.userLanguage);

    But some considerations here, Forminator has the load from the Ajax option, in case it is enabled we need to trigger the JS at a different moment because if we do just it on the footer the form doesn’t exist yet.

    To prevent that you can wrap the code with this PHP:

    <?php
    
    add_action('wp_footer', 'wpmudev_custom_form_js_script', 9999);
    function wpmudev_custom_form_js_script()
    {
    	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) {
    				jQuery('input[name="hidden-2"]').val(navigator.language || navigator.userLanguage);
    			});
    		});
    	</script>
    <?php
    }

    Another consideration is hidden-2 is too generic, I would suggest a verification with form_id or using a custom CSS selector:

    https://monosnap.com/file/fVWnuaa2nqMEDoT1nbYK0NMHo5SVf6

    This should fix the front end part https://monosnap.com/file/timDYhws8BNq0wGLg7XzfSv7JEPHdc

    There is a second issue since Forminator sanitizes the hidden field to prevent script injection, so it also requires this mu-plugin to modify the behaviour:

    <?php
    
    add_filter( 'forminator_prepared_data', 'wpmudev_update_hidden_field_val_copy', 10, 2 );
    function wpmudev_update_hidden_field_val_copy( $prepared_data, $module_object ){
        $form_ids = array(3588); //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] );
            }
        }
    
    	return $prepared_data;
    }
    
    add_action( 'forminator_custom_form_submit_before_set_fields', 'wpmudev_change_hidden_field_data_copy', 10, 3 );
    function wpmudev_change_hidden_field_data_copy( $entry, $module_id, $field_data_array ) {
        $form_ids = array(3588); //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']] );
            }
        }
    
    }

    Add the code as mu-plugin https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins

    you need to make some changes in the above code by replacing the hidden-1 with your hidden-ID and the 3588 to your form ID, with above steps then the JS code would work well: https://monosnap.com/file/19vmGLOK6IFS95Ef8ORCoQfyOhYifL

    Let us know if you have any additional questions.
    Best Regards
    Patrick Freitas

    Thread Starter killian6341

    (@killian6341)

    Thank you very much, I’ll test it when I’m back from holiday in a week and I’ll come back to you if I need anything.

    Plugin Support Kris – WPMU DEV Support

    (@wpmudevsupport13)

    Hi @killian6341

    Please let us know if can mark this thread as resolved?

    Kind Regards,
    Kris

    Plugin Support Amin – WPMU DEV Support

    (@wpmudev-support2)

    Hello @killian6341 ,

    We haven’t heard from you for over a week now, so it looks like you don’t have more questions for us.

    Feel free to re-open this topic if needed.

    Kind regards
    Kasia

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘When the value of a hidden field is updated by js, Forminator dnt retrieve it.’ is closed to new replies.