Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Update selected value on form submission (ACF) WordPress

  • REFUNDED

Hi all,

I've created a front-end WordPress user settings form and added some custom fields to it using the Advanced Custom Fields plugin. The code for this page checks if there were no issues on form submission and then redirects the user to the same page with updated field values.

I'm successfully able to fetch the values of all custom fields, as well as update the values of all custom fields, except one. It's a <select> element. I want the selected value to be updated in the backend upon form submission but it just won't work. Here is the code that I'm using to do this:


if ( !empty( $_POST['field_53207aa87753e'] ) ) {
$set_state = $_POST['field_53207aa87753e']; // Stores the selected <select> option value
update_field($state_fkey, $set_state, 'user_'.$current_user->ID);


Here is the output of the "$state" variable that's storing values for the <select> element:


Array
(
[key] => field_53207aa87753e
[label] => State
[name] => state
[_name] => state
[type] => select
[order_no] => 3
[instructions] =>
[required] => 1
[id] => acf-field-state
[class] => select
[conditional_logic] => Array
(
[status] => 0
[rules] => Array
(
[0] => Array
(
[field] => null
[operator] => ==
[value] =>
)

)

[allorany] => all
)

[choices] => Array
(
[AL] => Alabama
[AK] => Alaska
[AZ] => Arizona
[AR] => Arkansas
[CA] => California
[CO] => Colorado
[CT] => Connecticut
[DE] => Delaware
[DC] => District Of Columbia
[FL] => Florida
[GA] => Georgia
[HI] => Hawaii
[ID] => Idaho
[IL] => Illinois
[IN] => Indiana
[IA] => Iowa
[KS] => Kansas
[KY] => Kentucky
[LA] => Louisiana
[ME] => Maine
[MD] => Maryland
[MA] => Massachusetts
[MI] => Michigan
[MN] => Minnesota
[MS] => Mississippi
[MO] => Missouri
[MT] => Montana
[NE] => Nebraska
[NV] => Nevada
[NH] => New Hampshire
[NJ] => New Jersey
[NM] => New Mexico
[NY] => New York
[NC] => North Carolina
[ND] => North Dakota
[OH] => Ohio
[OK] => Oklahoma
[OR] => Oregon
[PA] => Pennsylvania
[RI] => Rhode Island
[SC] => South Carolina
[SD] => South Dakota
[TN] => Tennessee
[TX] => Texas
[UT] => Utah
[VT] => Vermont
[VA] => Virginia
[WA] => Washington
[WV] => West Virginia
[WI] => Wisconsin
[WY] => Wyoming
)

[default_value] => AL: Alabama
[allow_null] => 0
[multiple] => 0
[field_group] => 900
[value] => AR
)


I can properly echo the value of $set_state but when I'm trying to update it, it just won't update.

Please help!

Answers (1)

2014-03-16

Arnav Joy answers:

try this

update_field($state_fkey, $state['value'], 'user_'.$current_user->ID);


Avinash Kumar comments:

Thanks for your answer, Arnav, but it didn't work. It's still not updating the form from the frontend. I guess I'll just resort to Gravity Forms now. I tried to post to the ACF forum but it seems to be closed and I've already almost crossed the deadline.