Generate the input ids associated with the selected values from a multi-select checkboxes field [RESOLVED]

I am able to generate an array of values that a user has selected from a multi-select checkboxes field with the code below.

Can anyone explain how to generate the input ids associated with the values? These should be in the format 138.1, 138.2, 138.3 etc.


$user_locations_to_work_field_id = 138;
$user_locations_to_work = GFAPI::get_field( 15, $user_locations_to_work_field_id );

// Create a comma separated list of values e.g. Oldham, Salford, Stockport
$user_locations_to_work_value = is_object( $user_locations_to_work ) ? $user_locations_to_work->get_value_export( $entry, $user_locations_to_work_field_id ) : ''; 

// Create an array from the list of values outputs Array( [0] => Oldham [1] => Salford [2] => Stockport ) 
$user_locations_to_work_array = explode(',', $user_locations_to_work_value);

I can generate an array of all the input ids from the multi-select checkboxes field with the following code. However what I am looking for is the input ids associated with the values that a user has selected from a multi-select checkboxes field.

$user_locations_to_work_inputs = $user_locations_to_work->inputs;
// GFCommon::log_debug( __METHOD__ . '(): user_locations_to_work_inputs => ' . print_r( $user_locations_to_work_inputs, true ) );

I have looked through the documentation for using the get_value_export method of the field object Entry Object - Gravity Forms Documentation but it only mentions exporting the values and choices not the input ids.

Could the filter ‘gform_get_input_value’ be used to return the input ids?

Thanks to some invaluable help from Richard Wawrzyniak @richardw8k :+1: I was able to solve this with the following code. Hopefully this might help others looking to get both the input ids and the values from a multi-select checkboxes field :wink:

// Get locations that the user wants to work in multi-select checkboxes field 
$user_locations_to_work_field_id = 138;
$user_locations_to_work = GFAPI::get_field( 15, $user_locations_to_work_field_id );

// Create a comma separated list of values e.g. Oldham, Salford, Stockport
$user_locations_to_work_value = is_object( $user_locations_to_work ) ? $user_locations_to_work->get_value_export( $entry, $user_locations_to_work_field_id ) : ''; 

// Create an array with the checkbox input ids as the keys to their values
$user_locations_to_work_field_object = GFFormsModel::get_lead_field_value( $entry, $user_locations_to_work );

// If user has selected a location to work in then proceed
if($user_locations_to_work_value != '') { 
			
	foreach ( $user_locations_to_work_field_object as $input_id => $value ) {
				
		if ( empty( $value ) ) {
			continue;
		}
				
		// Add meta data with the inputs the user selected with their choice values assigned to them
		gform_add_meta( $wc_entry_id, $input_id, $value, 14 );
			
	}
					
}

1 Like

Thanks for sharing Rowan.