Dropdown selections using labels and values, want to show label not value in entry

I am trying to have the dropdown field show the label for the selected value in the entries, rather than the value. The field I want this to happen on, is the “agency location” field, of which there are about 30 but the form user will only see the one that corresponds to the agency they selected that they work for.

We have a form where a person selects the agency they work for, and then they select the specific location they work at for that agency that conditionally appears based on the first selection of agency. We have about 30 agency location dropdowns that are shown conditionally based on which agency a person selects. Also, each of these agency locations has a specific contact assigned to that location. In each dropdown we have selected to “show values” and the value for each location is the contact name assigned to that location.

This is done this way because in the confirmation email, we want to send the person who submitted the form a confirmation email with the location contacts name and contact information. So based on the location they work at, we needed the contact name associated with it.

Here is the code:

add_action( 'gform_pre_submission_20', 'ceiu_pre_submission_handler' );
function ceiu_pre_submission_handler( $form ) {

	// set rep name for hidden field id 86
	// first get the selection from previous question, Agency Name field id 26
	// this is necessary to know which agency location dropdown to use to get rep name
	$agency_name = rgpost('input_26');

	$agency_field_id = (int)null;
	$agency_field_label = '';
	
	// gets the field labels of every field in the form
	foreach ( $form['fields'] as $field ) {
		$agency_field_label = $field->label;

		// match field label to text string selected in $agency_name
		if ( str_contains( $agency_field_label, $agency_name ) ){
			$agency_field_id = $field->id;
			echo 'Agency Field Label: ' . $agency_field_label . '<br>';
			echo  'Dropdown Field ID: ' . $agency_field_id . '<br>';			
		}
	}
	
	// get value of agency location dropdown (the one selected on form conditionally)
	$agency_field_id = (string)$agency_field_id;
	$agency_field_input_id = 'input_' . $agency_field_id;
	echo '$agency_field_input_id: ' . $agency_field_input_id . '<br>';

	// gets *value of dropdown selected, which is *union rep name, sets value as rep name
	$union_rep_name = rgpost($agency_field_input_id);
	$_POST['input_86'] = $union_rep_name;

	// now change agency location back to label rather than showing the value
	$reset_dropdown_to_label = GFAPI::get_field( 20, $agency_field_id );
	var_dump($reset_dropdown_to_label);
}

The above code works correctly to get the selected agency value, use that to get the field ID of the conditionally shown dropdown for agency location. Then, it also correctly gets the value of the selected dropdown options which is the contact person’s name, it correctly saves this value to the hidden field for “representative” that will be used in the confirmation email. The problem is, the field selection in an entry for “agency location” does not show, rather it shows the value for that selection (value is the contact rep name).

I’m simply trying to get the conditionally shown dropdown to show it’s selected options label rather than it’s value in the entry, and on the exported CSV file. The code is returning the correct dropdown and saving it to the variable $reset_dropdown_to_label. I am having trouble finding how to access the dropdown selected option and retrieving it’s label. The var_dump shows the correct field object, but it also is not showing the correct option as being “isSelected”.

Shows the “agency location” field value, I want the label. The Union rep name is rendering correctly here:

Shows my selections on the two dropdowns:

Shows the field object after submitting, no value is shown as “isSelected”:

Hey @Thomas,

This code snippet shows Dropdown choice labels on the Entry Details page. See if that’ll help. :+1:

Hey @Jake_Jackson thanks for your reply. I tried that but it did not cause the desired result of the label of the option selected being displayed in the entry. The screenshot in my previous message is how they all appeared, in what I think is the entry detail page, even with that snippet in functions.php. I thought that I had interpreted what that snippet did incorrectly.

I’m using local by wpengine for dev, seeing that the snippet you linked to did not give the desired result, any idea why?

If you are dynamically populating your dropdowns, or saving a value for a non-existing choice, that could prevent the snippet from working.

I’d recommend creating a test form, adding a dropdown with differing choice labels/values, and enabling the aforementioned snippet. Verify the Entry Detail page shows the choice label in this form first. You can then start introducing the custom code you are doing on the other form, and testing as you go. That should help you narrow down the conflict. :+1:

I suspect @Jake_Jackson is on the right track with the dynamic population theory. If so, we solved this in Populate Anything by saving a copy of the selected choice in the entry meta and then reloading that choice into the form object using the same basic snippet Jake shared, just pulling from the entry meta instead.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.