• 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’ve been searching the web and the ACF forum/documentation for the past 2 days but can’t find a solution for this. I can properly echo the value of $set_state but when I’m trying to update it, it just won’t update.

    Please help!

    Thanks!

Viewing 3 replies - 1 through 3 (of 3 total)
  • exactly the same to me

    Thread Starter coddent

    (@coddent)

    If you still didn’t manage to find the solution for this, here is how I solved the problem:

    I added a $session_start(); at the very top of the file. Then under the test block to see if the form was submitted, I added this:

    $_SESSION = $_POST;

    Then modified the original code to this:

    if(isset($_SESSION['field_53207aa87753e'])) {
          $set_state = $_SESSION['field_53207aa87753e'];
          $state_fkey = "field_53207aa87753e";
          update_field($state_fkey, $set_state, 'user_'.$current_user->ID);
        }

    Solved the issue. There might be a better way to do this which I might try later when I’ve time but right now, I really needed to solve this ASAP. I think the redirect is causing $_POST[‘field_53207aa87753e’] to stay empty while adding a session variable solves this.

    LeRedac

    (@leredac)

    Hello,

    try

    if ( !empty( $_POST["fields"]['field_53207aa87753e'] ) ) {

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Update selected value on form submission (ACF)’ is closed to new replies.