Saving values in dropdown vie php doesn't show up while editing

By: Merlin Rose | Asked: 06/07/2022
ForumsCategory: Code HelpSaving values in dropdown vie php doesn't show up while editing
Merlin Rose asked 2 years ago

Hello community,
i have a problem with saving values in a dropdown via php. I use the following code to add or update a value in field 44.

Start your code here
$account_id = FrmProEntriesController::get_field_value_shortcode(array('field_id' => 42, 'user_id' => $user_id)); // UPDATE ACCOUNT UND TICKETS // Zeitnahmedaten (Geschlecht, Geb) $user_account = FrmEntry::getOne($account_id, true); $update_account = false; $geschlecht = get_post_meta( $order->id, 'Geschlecht', true ); if ( isset($user_account->metas[44]) && !empty($geschlecht) ) { // 44 = Geschlecht FrmEntryMeta::update_entry_meta($account_id, 44, null, $geschlecht); $update_account = true; } elseif ( !empty($geschlecht) ) { FrmEntryMeta::add_entry_meta($account_id, 44, null, 'Herr'); FrmEntryMeta::add_entry_meta($account_id, 407, null, 'Herr'); // New test field (text) FrmEntryMeta::add_entry_meta($account_id, 408, null, 'Herr'); // New test field (dropdown) $update_account = true; }

The value is saved in the database and it shows in views and in the backend tables, but when i want to edit the entry, the value doesn\'t show up in the dropdown field.
This is what i tried so far:

    • Changed the value in phpMyAdmin » Value changed in views but not prefilled while editing (empty dropdown). Yes i make sure, that the value is an option of this field!

 

    • Created new fields (text and dropdown), saved the same value vie code snipped to exclude formatting failures and the value are saved correctly and showed (!) up while editing in all new fields.

 

    • Change the settings of the field in backend and copied the values from the new field via phpMyAdmin » same problem.

 

    • Changed from dropdown to radio buttons » same problem.

 

    • Chenged from dropdown to checkboxes » value showed up while editing.

 

    • the only way to show a value while editing is to save the from again with the right value. Then i have the dropdown prefilled the next time i want to edit.

Does anybody has this problem before or any idea how i can replace this field and not changing the field id?
My workaround will be to crate a new field and copy all values to the new field and change the field id everywhere in code. 
 
Thanks everybody for help.

2 Answers
Victor Font answered 2 years ago

First, it's difficult to understand what you are trying to do because your code is incomplete. You also don't mention 44's field type. Is it a Dynamic, Lookup, or static drop down?

If you are using a Dynamic or Lookup dropdown, you can add a new value as you want by inserting the new record into the lookup table, not the table that receives the selected value(s).

If you are using a static drop down, which is one where you've added the values manually or through bulk edit in the form builder, you're basically out of luck without advanced PHP knowledge. Static dropdowns store their values in the wp_frm_fields table as a PHP serialized array. This is an example of serialized array content as stored in the database:

a:8:{i:0;a:2:{s:5:"label";s:8:"Under 18";s:5:"value";s:8:"Under 18";}i:1;a:2:{s:5:"label";s:5:"18-24";s:5:"value";s:5:"18-24";}i:2;a:2:{s:5:"label";s:5:"25-34";s:5:"value";s:5:"25-34";}i:3;a:2:{s:5:"label";s:5:"35-44";s:5:"value";s:5:"35-44";}i:4;a:2:{s:5:"label";s:5:"45-54";s:5:"value";s:5:"45-54";}i:5;a:2:{s:5:"label";s:5:"55-64";s:5:"value";s:5:"55-64";}i:6;a:2:{s:5:"label";s:11:"65 or Above";s:5:"value";s:11:"65 or Above";}i:7;a:2:{s:5:"label";s:20:"Prefer Not to Answer";s:5:"value";s:20:"Prefer Not to Answer";}}

To add something to a serialized array, you have to retrieve the value from the database, unserialize() it in PHP, add the value to the array, serialize() it again, and save it back to the database. See https://www.php.net/manual/en/function.serialize.php for more information.

Victor Font replied 2 years ago

The bottom line is to use Dynamic or Lookup fields if you want to add values on demand.

Merlin Rose answered 2 years ago
Hi Victor, thanks for your answer. I try to simplify my question. It not really rely on the code itself but first things first. In my Woocommerce checkout i created a new field with gender and have the values "Herr" and "Frau" (Mr & Mrs in german). After the order i get these values and save them into my formidable form in the field 44. 44 is just a simple dropdown with the values pre-defined. This method works in other installations and in other cases, when i want to save or update values in fields. But here i noticed that the value is saved to the database and is shown in the entries list but will NOT show up if i want to edit the entry where i saved the value before. The result after a woo commerce order is the value in all lists but empty dropdown while editing. The only way to save the value is to select it again in the form and save it. So after that i tested around and change the value manually in the database from "Herr" to "Frau". The result is that the new value "Frau" is shown in all lists but "Herr" is still selected when i edit this entry. So in my understanding of a database this makes no sense. I tested other fields and a new created dropdown and here the values are saved correctly and showed up while editing like expected. So i think something is wrong with the field settings of 44 and my question is more how to reset a field or copy fresh settings to it without creating a new field without changing the field id. In the future i will defiantly go to just use the field keys only in my code. This should make all fields replaceable right? I attach some screenshots for better understanding.   Thanks a lot, Merlin
Attachments

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right