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, 5 months ago by
michaelbruch.
-
This topic was modified 1 year, 5 months ago by
-
Hello Nithin, thank you very much for your help.
The code works so far. Unfortunately, it causes an error with another hidden field. Is it possible to use another hidden field outside of hidden-1 to hidden-4?
I need the hidden-5/or 6/or 7 field for the entry ID. I need this ID in the CSV table. In the CSV, I only get the following value returned and not the ID:
Hidden: submission_id
The correct ID is displayed in the user’s email with the shortcode {submission_id}, and the ID is also shown in the subject of the email, just not in the CSV. When I deactivate the plugin, the ID works in the CSV.
This is the table from the database. The value from entry_id should also be in meta_value:
-
This reply was modified 10 months, 2 weeks ago by
michaelbruch. Reason: add Image
Hi @michaelbruch,
Hope this message finds you well.
Well, the tag {submission_id} is for the Notification Email, if you want to get the submission ID in the Hidden field you only need to select it from the dropdown:
If this is not what you are looking as requested before, could you export your form using the guide on this link: https://wpmudev.com/docs/wpmu-dev-plugins/forminator/#import-export? Once exported, kindly use a Cloud service like Google Drive, pastebin.com, or similar to share it. If you have sensitive data, please duplicate the form, remove it, and export the form.
Let us know the results.
Best regards,
LauraHello Laura, thank you very much for your response. I know all of that. In the hidden field, I have also selected the Submission ID. It works even when the mu_plugin from this forum thread is not activated.
The mu-plugin fills hidden-1 to 4. However, I have selected the Submission ID in hidden 5, but it doesn’t work when the mu-plugin is activated.
Thank you for response!
I suspect it might be related to certain bug in the plugin but just to make sure: is it
a) only happening if MU plugin is active and does not if it’s not activated? Or is it happening always? (I’m sorry if I’m asking the obvious but it’s not quite clear to me based on your post)
b) is it only happening in data exported (via integration) to Google Sheet or it’s also in e-mail notification and/or form submission on “Forminator -> Submissions” page?
Kind regards,
AdamHello Adam,
a) only happening if MU plugin is active and does not if it’s not activated? Or is it happening always? (I’m sorry if I’m asking the obvious but it’s not quite clear to me based on your post)
Yes, it only happens if the MU plugin is active and does not occur if it’s not activated.
b) is it only happening in data exported (via integration) to Google Sheet or it’s also in e-mail notification and/or form submission on “Forminator -> Submissions” page?
In the data export via integration to Google Sheets, I haven’t tested it. It’s not being used. We export via manual export to CSV. It works in email notifications via the shortcode. It doesn’t work in a hidden field in the form and therefore is not exported as a record in the CSV file.
Other data in hidden fields such as IP address, user agent, form URL work without issues. Only the submission ID doesn’t.
Here’s a screenshot of how it looks in the Submissions Backend. Shortened and censored:
View post on imgur.com
Hi @michaelbruch ,
Thanks for providing further information, are there any changes made in the code snippet provide and also the form based on hidden-5/6/7?
If yes, possible to share the current updated code which you have as the mu-plugins and also re-share a new form export so that we could run additional test to have a better idea?
Please check the following doc on how to export a form:
https://wpmudev.com/docs/wpmu-dev-plugins/forminator/#import-export
If 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 service in the next reply.
Looking forward to your response.
Best Regards,
Nithin
Hello Nithin,
yes, I have adjusted and expanded the code from the MU plugin to include hidden-5.
I don’t think that should be the problem. When I use your original code without the hidden-5 extension, it also doesn’t work.I have incorporated the hidden-6 field as a test with the href URL, and hidden-7 is the non-functioning submission ID.
I could access the form backup and the MU plugin PHP on my SharePoint:
WPMUDEVThanks for response and sharing both form and code.
I checked it and yes – MU plugin code is causing it because it changes the way hidden fields are handled. There needs to be a small adjustment made.
This part of the code
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']]); } } }
needs to be changed to this:
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) { if ($key !== 'hidden-7' ) { $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) { if ( $value['name'] !== 'hidden-7' ) { Forminator_CForm_Front_Action::$info['field_data_array'][$key]['value'] = sanitize_text_field($_POST[$value['name']]); } } } }
You also need to make sure that the “hidden-7′ in both places is replaced with the ID of the hidden field that is set to have submission_id in your form. The form that you shared uses “hidden-7” field for that so that’s why I set it like this.
In general, the goal here is to make that field an exception from being handled by the code.
Best regards,
AdamHello Adam, many thanks for adjusting the code.
The fix works wonderfully.
Best regards to the entire team.
I’m glad I could help and thank you for your kind words!
I’ll mark it as resolved for now but in case you’d have any additional questions about this, just let us know and we’ll be happy to help.
Best regards,
Adam -
This reply was modified 10 months, 2 weeks ago by
- The topic ‘Automatically filled hidden fields’ is closed to new replies.