• Resolved greatpage

    (@greatpage)


    Hello, Forminator was recently showed to me, and it’s so great. I started changing all my sites over to it.

    I just ran into a slightly more complex use case, where I want to include a value from localStorage in my form submission. To do this, I figured I would use a custom hidden field. Once the page loads, I use a javascript snippet to look for the field by name and update the value, then a few seconds later, I read the value to make sure it worked.

    <script>
    	setTimeout(() => {
    		console.log('looking for the value');
    		var hiddenElement = document.getElementsByName('hidden-1');
    
    		if (hiddenElement) {
    			var val = localStorage.getItem('utm_source')
    			console.log('updating element to: ', val);
    			hiddenElement.value = val;
    		} else {
    			console.error('Element not found!');
    		}
    	}, 2000);
    	
    	setTimeout(() => {
    		console.log('looking for the value');
    		var hiddenElement = document.getElementsByName('hidden-1');
    		console.log('after 5', hiddenElement.value)
    		
    	}, 5000);
    	
    </script>

    It does seem to change the value client side, but the email and submission have the original value of the custom field.

    Has anyone tried something like this successfully, or see where I might be missing something?

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

    (@wpmudevsupport13)

    Hi @greatpage

    I hope you are doing well today.

    I pinged our SLS Team to review your query and see what they can do in that matter to help you. We will post an update here as soon as more information is available.

    Kind Regards,
    Kris

    Plugin Support Kris – WPMU DEV Support

    (@wpmudevsupport13)

    Hi again @greatpage

    You can try this code as a mu-plugin where form ID?6?should be changed:

    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] );
            }
        }
    
    	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']] );
            }
        }
    }

    You can find more information below on how to use mu-plugins.
    https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins

    Kind Regards,
    Kris

    Plugin Support Amin – WPMU DEV Support

    (@wpmudev-support2)

    Hello @greatpage ,

    I will assume that this code from Kris worked for you so I will close this topic.

    If you need further assistance let us know.

    kind regards,
    Kasia

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Forminator Hidden Field Populated By Local Storage Value’ is closed to new replies.