Automatically filled hidden fields
-
Is it possible to create hidden fields in the form that automatically assume a specific value based on the selection of another field? Example:
if Selection:
Branch 1
Branch 2
Branch 3
Branch 4then Hidden Field “#”:
56
75
89
14then Hidden Field “Location”:
City 1
City 2
City 3
City 4The additional hidden fields are important for email management to help expedite the process of assigning them. The form user should only select the branch, and based on that, the hidden fields should be added to the email for the admin email and included in the CSV export.
Unfortunately, I couldn’t find a similar task in the forum.
-
This topic was modified 1 year, 1 month ago by
michaelbruch.
-
This topic was modified 1 year, 1 month ago by
-
Hi @michaelbruch,
I hope you are doing well today!
In order to understand your logic better can you please confirm if this is correct.
If Branch 2 is selected then Hidden Field # will be 75 then Location will be City 2 and you need those 2 hidden fields to be sent to admin email.
Kind regards,
ZaferHello, thank you for asking, yes – I hope you are too.
Yes, exactly like that. Additionally, the hidden fields should also appear in the CSV export.
Hi @michaelbruch,
Thanks for the follow-up. Could we know whether the options for these Select fields i.e. Branch 1, 2 etc. will there be more additional branches added later?
Or is it going to be specifically the 4 branches as you have mentioned in the initial response?
Unfortunately, such a workflow isn’t supported out of the box on the plugin side i.e. to dynamically change the values of the hidden field based on selection and adding more branches can bring more complexities.
So we’ll have to check whether this could be achieved using custom coding and hence double checking the workflow you are looking to implement in order to see what could be suggested further.
Please advise. Looking forward to your response.
Kind Regards,
Nithin
Hello Nithin, thank you for the quick response.
This was just an example.
In practice, there are currently 28 schools in a dropdown menu. More may be added in the future. There should be 4 hidden data fields for each: school number, street, postal code, and city.
I believe this makes it a bit more complicated now. I chose a simple example to initially explain it more straightforwardly.
Hi @michaelbruch,
As mentioned in our previous response, Forminator does not have an out-of-the-box solution that could help with this. However, I have pinged our developers to see if a workaround could be provided. We’ll update you here once we have more feedback on this as soon as possible.
Kind Regards,
Nebu JohnHello @michaelbruch ,
Please try this code as mu-plugin (https://premium.wpmudev.org/docs/getting-started/download-wpmu-dev-plugins-themes/#installing-mu-plugins )
add_action('wp_footer', 'wpmudev_hidden_field_value_update', 9999); function wpmudev_hidden_field_value_update(){ 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 the form ID var school_arr = {}; school_arr['Branch 1'] = ['123', 'street 1', '4567', 'BBK']; //Please add school info school_arr['Branch 2'] = ['456', 'street 2', '8901', 'KBB']; //Please add school info $('#select-1 select').on('select2:select', function(e) { var select_val = $(e.currentTarget).val(); $('input[name="hidden-1"]').val(school_arr[select_val][0]); $('input[name="hidden-2"]').val(school_arr[select_val][1]); $('input[name="hidden-3"]').val(school_arr[select_val][2]); $('input[name="hidden-4"]').val(school_arr[select_val][3]); }); } }); }); </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); if ( !in_array( $module_object->id, $form_ids ) ) { return $prepared_data; } foreach ( $prepared_data as $key => $value ) { if ( strpos( $key, 'hidden-' ) !== 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-' ) !== false ) { Forminator_CForm_Front_Action::$info['field_data_array'][$key]['value'] = sanitize_text_field( $_POST[$value['name']] ); } } }
You need to add school branches like this:
school_arr['Branch 1'] = ['123', 'street 1', '4567', 'BBK']; //Please add school info school_arr['Branch 2'] = ['456', 'street 2', '8901', 'KBB']; //Please add school info
Branch 1 and Branch 2 should be the same as the select field option value(not label). Form IDs should be changed too.
kind regards,
KasiaHello Kasia, thank you very much for your help. I’ve adjusted and integrated the code.
Unfortunately, I’m encountering an error on the front end of the website that says, “There was a critical error on your website.”
Do I need to replace just the “6” in the form ID “forminator-module-6” or the entire value? Similarly, there is a form ID in “add_filter section”
$form_ids = array(6);
present, but there is no comment following it.
I have tested all variations with the form ID, but I keep getting the same error. For a test, I have only entered two schools. Or do all the selection fields need to be filled in?
Best regards, Michael.
Hi @michaelbruch,
I’m encountering an error on the front end of the website that says, “There was a critical error on your website.”
I checked the code but I couldn’t find the code that will be throwing a critical error out of the box. I suppose you have added the code as a mu-plugin, right?
If yes, could you please make sure the code starts with the line <?php, ie for example:
<?php add_action('wp_footer', 'wpmudev_hidden_field_value_update', 9999); // rest of the code
Do I need to replace just the “6” in the form ID “forminator-module-6” or the entire value??
Only replace the number 6 with your form ID. Suppose the form ID Is 123, then the following line will change from:
if ( e.target.id == 'forminator-module-6' )
to:
if ( e.target.id == 'forminator-module-123' )
The same would be for the following only change number 6 to form ID:
$form_ids = array(6);
However, could you please share the form export where you are testing the code out? So that we could have a better idea regarding the issues noticed?
Please check the following doc on how to export a form:
https://wpmudev.com/docs/wpmu-dev-plugins/forminator/#import-exportIf you are concerned about any sensitive information in the form, then you can duplicate your form, remove any sensitive information, and then export it.
You can share the export file via Google Drive, Dropbox or any cloud services in the next reply.
Looking forward to your response.
Best Regards,
Nithin
Hi @michaelbruch,
We haven’t heard from you in a while, we will go ahead and mark this thread as resolved. If you have any additional questions or require further help, please let us know!
Kind regards,
ZaferHello everyone, I’m sorry that I have to bring up the topic again.
Due to other projects, I had to pause this one initially. Now I’ve tried the code again, and this time I didn’t encounter any error message. That was obviously a beginner’s mistake.
However, unfortunately, the desired result is still not implemented in the form. I have entered two sample data in the code and adjusted the form ID. In the form, I have created 4 hidden fields. The IDs of the fields match those in the code. Is it correct that I have to create the hidden fields myself in the form? If yes, I am unsure what default value needs to be set in the hidden field.
After submitting the form, the fields remain empty or, depending on the default value set, it is entered in the field.
Here is the code used:
<?php add_action('wp_footer', 'wpmudev_hidden_field_value_update', 9999); function wpmudev_hidden_field_value_update(){ 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-1889' ) { //Form ID var school_arr = {}; school_arr['Altenstadt'] = ['7853', 'Schillerstrasse 2', '75897', 'Musterstadt']; //Schule school_arr['Nauheim'] = ['4582', 'Am Solgraben 8', '58785', 'Neueschule']; //Schule $('#select-1 select').on('select2:select', function(e) { var select_val = $(e.currentTarget).val(); $('input[name="hidden-1"]').val(school_arr[select_val][0]); $('input[name="hidden-2"]').val(school_arr[select_val][1]); $('input[name="hidden-3"]').val(school_arr[select_val][2]); $('input[name="hidden-4"]').val(school_arr[select_val][3]); }); } }); }); </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(1889); if ( !in_array( $module_object->id, $form_ids ) ) { return $prepared_data; } foreach ( $prepared_data as $key => $value ) { if ( strpos( $key, 'hidden-' ) !== 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(1889); //Form ID if ( !in_array( $module_id, $form_ids ) ) { return; } foreach ( $field_data_array as $key => $value ) { if ( strpos( $value['name'], 'hidden-' ) !== false ) { Forminator_CForm_Front_Action::$info['field_data_array'][$key]['value'] = sanitize_text_field( $_POST[$value['name']] ); } } }
Here is my form exported:
https://hastebin.com/share/minuxunupe.swift
Thank you.
Best greetings.
-
This reply was modified 8 months, 3 weeks ago by
michaelbruch.
-
This reply was modified 8 months, 3 weeks ago by
michaelbruch.
I hope you are doing well.
The code seems to be correct, in your Form the Hidden fields should be “custom value” rather than the “User ID”, but I see that even though fixing it the value is not added to the field.
We forwarded it to our second-line support agent so we could run some further testing with the code.
We will keep you posted.
Best Regards
Patrick FreitasCan you please try this version?
<?php add_action('wp_footer', 'wpmudev_hidden_field_value_update', 9999); function wpmudev_hidden_field_value_update() { 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-1889') { //Form ID var school_arr = {}; school_arr['Altenstadt'] = ['7853', 'Schillerstrasse 2', '75897', 'Musterstadt']; //Schule school_arr['Nauheim'] = ['4582', 'Am Solgraben 8', '58785', 'Neueschule']; //Schule $(document).on('select2:select', '#select-1 select', function(e) { var select_val = $(e.currentTarget).val(); $('input[name="hidden-1"]').val(school_arr[select_val][0]); $('input[name="hidden-2"]').val(school_arr[select_val][1]); $('input[name="hidden-3"]').val(school_arr[select_val][2]); $('input[name="hidden-4"]').val(school_arr[select_val][3]); }); } }); }); </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(1889); if (!in_array($module_object->id, $form_ids)) { return $prepared_data; } foreach ($prepared_data as $key => $value) { if (strpos($key, 'hidden-') !== 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(1889); //Form ID if (!in_array($module_id, $form_ids)) { return; } foreach ($field_data_array as $key => $value) { if (strpos($value['name'], 'hidden-') !== false) { Forminator_CForm_Front_Action::$info['field_data_array'][$key]['value'] = sanitize_text_field($_POST[$value['name']]); } } }
Along with changes in the Hidden field > Select “custom value”
You can see it working fine on my lab site https://monosnap.com/file/Wypvb1K3JPzDJchaYDzkOQeZGFb0mN
Best Regards
Patrick FreitasThank you very much for the code. It works great!
I have two more question:
- Is it possible to apply this code simultaneously to multiple forms? The form is duplicated every month, customized, and then published on the website according to a schedule. In my workflow, when duplicating and customizing, I can always add the new form ID. Or even globally for all forms? Then I wouldn’t need to adjust it ever.
- Would it also be possible to integrate a second different dropdown list where additional data is queried and built into other hidden fields?
Hello @michaelbruch,
1. Is it possible to apply this code simultaneously to multiple forms? … Or even globally for all forms? Then I wouldn’t need to adjust it ever.
Yes, it should be possible to apply this code snippet to all forms. However, in this case it will also be applied even to Forminator forms with different structure, potentially causing undesired changes.
Rather than applying it to any form, please consider the following modified version of the previously shared snippet, which should be applied to forms only on a specific post/page:
<?php $form_page_id = 123; add_action('wp_footer', 'wpmudev_hidden_field_value_update', 9999); function wpmudev_hidden_field_value_update() { global $post, $form_page_id; if (is_a($post, 'WP_Post') && $post->ID != $form_page_id && !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.includes('forminator-module-')) { var school_arr = {}; school_arr['Altenstadt'] = ['7853', 'Schillerstrasse 2', '75897', 'Musterstadt']; //Schule school_arr['Nauheim'] = ['4582', 'Am Solgraben 8', '58785', 'Neueschule']; //Schule $(document).on('select2:select', '#select-1 select', function(e) { var select_val = $(e.currentTarget).val(); $('input[name="hidden-1"]').val(school_arr[select_val][0]); $('input[name="hidden-2"]').val(school_arr[select_val][1]); $('input[name="hidden-3"]').val(school_arr[select_val][2]); $('input[name="hidden-4"]').val(school_arr[select_val][3]); }); } }); }); </script> <?php } add_filter('forminator_prepared_data', 'wpmudev_update_hidden_field_val', 10, 2); function wpmudev_update_hidden_field_val($prepared_data, $module_object) { global $post, $form_page_id; if (is_a($post, 'WP_Post') && $post->ID != $form_page_id && !has_shortcode($post->post_content, 'forminator_form')) { return $prepared_data; } foreach ($prepared_data as $key => $value) { if (strpos($key, 'hidden-') !== 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) { global $post, $form_page_id; if (is_a($post, 'WP_Post') && $post->ID != $form_page_id && !has_shortcode($post->post_content, 'forminator_form')) { return; } foreach ($field_data_array as $key => $value) { if (strpos($value['name'], 'hidden-') !== false) { Forminator_CForm_Front_Action::$info['field_data_array'][$key]['value'] = sanitize_text_field($_POST[$value['name']]); } } }
Change 123 to the actual page/post ID on this line at the beginning:
$form_page_id = 123;
2. Would it also be possible to integrate a second different dropdown list where additional data is queried and built into other hidden fields?
We’ll have to check with the SLS team, whether re-using parts of the existing code for the second list and the new set of fields would be possible. Please note however, that if too many changes, or extra programming is required to achieve the desired behavior, this may appear out of our support scope.
Could you please export an example of the form, in order for our techs to review it and check if we could help you with the changes.
Best Regards,
DmytroHi @michaelbruch,
Since we haven’t heard from you for a while. I’ll mark this thread as resolved for now. Please feel free to open a new thread if you have new queries.
Kind Regards
Nithin -
This reply was modified 8 months, 3 weeks ago by
- The topic ‘Automatically filled hidden fields’ is closed to new replies.