Support Forum
Hi,
I have 2 select2 dropdowns namely company_name
and plan_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');
});
});
I am building this form in Frontend submission extension
Hello,
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/
Hi,
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.
Hi,
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.
Hello,
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/
Also, please try to disable the sanitization and check the save value process again. Following the documentation https://docs.metabox.io/sanitization/#bypass-the-sanitization
Hey Aishwarya,
did you find a method to programmtically add values to the select2 in the frontend? could you elaborate that?
Kind regards