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