skip to Main Content

I have the following function with an array of variables that handles a profile form. All but one is a typical input with the exception of 'member-comptency[]'. It’s a select field field with several possible values. Right now, the form saves, all the data comes through as expected with the exception of that field which simply returns a number which I assume is the html value. In looking at it a bit more, the "value" of the element is simply a number also which might make matters worse.

I’ve tried in vain several times to modify this in different ways, but only produces a ‘no-save’ situation, or further non-descrip errors. I understand this field is an array itself, but not sure how to even ask a better question to get myself on track.

The function

function getUMFormData(){
    $id = um_user('ID');
        $names = array(
            'member-company',
            'member-phone',
            'member-website',
            'member-competency[]',
            'member-contact-billing-name'
        );

        foreach( $names as $name )
        update_user_meta( $id, $name, $_POST[$name] );
}

The HTML element

One of several choices

<label class="um-field-radio  um-field-half  right ">
  <input type="radio" name="member-competency[]" value="1">

  <span class="um-field-radio-state">
    <i class="um-icon-android-radio-button-off"></i>
  </span>

  <span class="um-field-radio-option">Manufacturer</span>
</label>

The desired outcome

I need to get the the word from the last <span> element (Manuracturer in the example) to save into the getUMFormData() function.

2

Answers


  1. I suppose ‘member-competency[]’ is not the right way to access an array of values within the post array. You could try to var_dump() or print_r() the $_POST[‘member-competency’]; See if the array is displayed properly.

    Login or Signup to reply.
  2. From your HTML example, note that the "Manufacturer" radio button has "1" as value.

    Now, in your function, the meta key to be used is 'member-competency' without [].

    Then note that $_POST['member-competency'] will give an array like array('1') because of [] in member-comptency[] attribute name from the input radio button HTML.

    If you get array('1') from $_POST['member-competency'] is that the selected radio button is "Manufacturer", and you can set "Manufacturer" as meta value.

    So your function will be:

    function getUMFormData(){
        $user_id = um_user('ID');
    
        $fields  = array(
            'member-company',
            'member-phone',
            'member-website',
            'member-competency', // <== Removed "[]"
            'member-contact-billing-name'
        );
    
        foreach( $fields as $meta_key ) {
            if ( $user_id > 0 && isset($_POST[$meta_key]) ) {
                $meta_value = $_POST[$meta_key];
                // Targeting 'member-competency' field with "Manufacturer" label
                if ( $meta_key === 'member-competency' && is_array($meta_value) && current($meta_value) == '1' ) {
                    update_user_meta( $user_id, $meta_key, __("Manufacturer") );
                } 
                // Other fields
                else {
                    update_user_meta( $user_id, $meta_key, esc_attr($meta_value) );
                }
            }
        }      
    }
    

    It should work.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search