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:
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.
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.
The bottom line is to use Dynamic or Lookup fields if you want to add values on demand.
Please login or Register to submit your answer