Support

Account

Home Forums Front-end Issues Select field based on other fields value

Solving

Select field based on other fields value

  • I have created two text fields that will display on a registration form.

    After the registration based on these two text fields I want to display their values as a select field.

    Is this possible? Is there a way to create a select field which will take choices based on another fields values?

    I can display these fields on a select field with the following code but the problem here is that I cannot save the choise value.

    
    <select name="cars" id="cars">
    	<option value=" <?php the_field('car_type_1'); ?> " > <?php the_field('car_type_1'); ?> </option>
    	<option value=" <?php the_field('car_type_2'); ?> " > <?php the_field('car_type_2'); ?> </option>
    </select>
    
  • After doing some research I think that the best way to achieve this is by creating a repeater field with a text subfield.

    The question now is how can I display this repeater field as a select field with the values of the text subfields and how can I save the value from the select field choise (the repeater field will display in a registration form).

  • UPDATE
    So I’ve managed to display the repeater field as a select box with the following code

    <select name="cars" id="cars">
    <?php
    // Check rows existexists.
    
        // Loop through rows.
        while( have_rows('yacht_type1') ) : the_row();
    
            // Load sub field value.
            $sub_value = get_sub_field('name');
            // Do something...
    	
    		echo '<option value="'.$sub_value.'">'.$sub_value.'</option>';
    	endwhile;
    ?>
    </select>

    Now what I’m actually searching for is how to get and store the value from the select box.

  • I’m not sure I understand completely what you are trying to do, so this may or may not be helpful.

    Created an options page. On the options page you have a repeater with a text sub field. You enter all of the possible values that you want in the select field. Then you create a select field where you want to use these values and you dynamically populate the choices.

  • Actually you helped a lot.
    I was trying to populate the choises from a field group that was showing on users.

    Is it possible to do this on a field group that is showing somewhere else besides options page?
    I’m trying to show this field group on users panel but the populate functions doesn’t work there.

  • Finally, I’ve managed to succesfully populate the repeater field in a select field in every users panel with this function

    function acf_load_select_field_field_choices($field) {
        global $current_user;
        //Get the repeater field values
        $choices = get_field('repeater_field_name',$current_user);
    	$field['choices'] = [];
        // loop through array and add to field 'choices'
        if (is_array($choices)) {
            foreach ($choices as $choice) {
                //Set the select values
                $field['choices'][$choice['subfield']] = $choice['subfield'];
            }
        }
        // return the field
        return $field;
    }
    add_filter('acf/load_field/name=select_field', 'acf_load_select_field_field_choices');

    The only problem that I’m facing right now is that the select field doesn’t update when submitting a form.

    The code that I’m using to display and update the select field is the following

    <?php 
       $field = get_field_object('repeater_field_key');
          if (!empty($field['choices'])) { ?>
               <select>
                    <?php foreach ($field['choices'] as $v => $l) { ?>
                        <option value="<?php echo $v; ?>"><?php echo $l; ?></option>
                    <?php } ?>
               </select>
          <?php } 
          $current_user_id = get_current_user_id();
          update_field($field, $field['value'], 'user_'.$current_user_id); 
    ?>

    What am I doing wrong?

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.