Dynamically populating options values for select2 doesn't save to database
- This topic has 7 replies, 3 voices, and was last updated 1 year ago by
Bomes.
-
AuthorPosts
-
March 13, 2024 at 11:04 PM #44842
Aishwarya
ParticipantHi,
I have 2 select2 dropdowns namely
company_name
andplan_name
. The plan_name is set of values for different company_name. So I am populating the second dropdown using the below jQuery code by appending new values to the plan_name select2.The dropdown selection is just working fine when selecting a company_name and the corresponding set of plan_name (s) is populated dynamically. But when I save the form, the selected plan_name value is not saved to the database. It is always empty. The problem is only with the dynamic population of the plan_name values.
I tried $('#mySelect2').append(newOption).trigger('change'); from https://select2.org/programmatic-control/add-select-clear-items but not works. Can you please help me what I am doing wrong here?
jQuery(document).ready(function($) { $("#company_name").on("change", function() { var selectedValue = $(this).val(); // Preparing plan name options var options = {}; if (selectedValue === "tatasons") { options = { <?php $tatasons_list = tatasons_list(); if ( !empty($tatasons_list) && is_array($tatasons_list)) { foreach ( $tatasons_list as $key => $value ) { echo "'$key': '$value',"; } } ?> }; } else if (selectedValue === "tatachem") { options = { <?php $tatachem_list = tatachem_list(); if ( !empty($tatachem_list) && is_array($tatachem_list)) { foreach ( $tatachem_list as $key => $value ) { echo "'$key': '$value',"; } } ?> }; } else if (selectedValue === "birlawhite") { options = { <?php $birlawhite_list = birlawhite_list(); if ( !empty($birlawhite_list) && is_array($birlawhite_list)) { foreach ( $birlawhite_list as $key => $value ) { echo "'$key': '$value',"; } } ?> }; } // Populate the second dropdown with the prepared options $("#plan_name").empty(); // Clear existing options $.each(options, function(key, value) { $("#plan_name").append($('<option></option>').val(key).text(value)).trigger('change'); }); });
March 13, 2024 at 11:05 PM #44843Aishwarya
ParticipantI am building this form in Frontend submission extension
March 14, 2024 at 8:38 PM #44854Peter
ModeratorHello,
The problem is the select/select advanced field only saves value from options when you register the field. It doesn't save the value that is populated by JavaScript code. For example:
[ 'name' => 'Select Advanced', 'id' => 'select_advanced', 'type' => 'select_advanced', 'options' => tatasons_list(), ],
Please follow the documentation https://docs.metabox.io/fields/select-advanced/
March 14, 2024 at 11:35 PM #44862Aishwarya
ParticipantHi,
Yes, I can understand but select2 does support for values populated by JS as mentioned here https://select2.org/programmatic-control/add-select-clear-items
If you let me know which field I need to update on change event, so I can try to get the new selected value and insert to the field that is submitted on POST.
March 15, 2024 at 5:01 PM #44867Aishwarya
ParticipantHi,
When print the values submitted by the form, the values of the form are corrected submitted. i.e., the values of the company_name and plan_name populated by the javascript code are correctly submitted.
But I don't know why the Metabox is not saving it to the post meta. It seems like there is a filter for select and select advanced before saving it to the database which is preventing it from save.
Can you please let me know which file I should check where the form data is filtered and saved to database? So I can try to save the values by any custom code method.
March 15, 2024 at 9:43 PM #44876Peter
ModeratorHello,
You can try to use the filter hook
rwmb_after_save_field
to update the field value from the $_POST variable. However, I'm not sure if it can work with your custom JS code to show the saved/current value after saving the post.Following the documentation https://docs.metabox.io/actions/rwmb-after-save-field/
March 15, 2024 at 10:22 PM #44878Peter
ModeratorAlso, please try to disable the sanitization and check the save value process again. Following the documentation https://docs.metabox.io/sanitization/#bypass-the-sanitization
April 16, 2024 at 9:13 PM #45229Bomes
ParticipantHey Aishwarya,
did you find a method to programmtically add values to the select2 in the frontend? could you elaborate that?
Kind regards
-
AuthorPosts
- You must be logged in to reply to this topic.