Autopopulate hidden field depending on chosen option from a dropdown

Hi! I would like your help with the following.

We are using Gravity Forms for out application form. Within this form there is a (mandatory) dynamic dropdown that lists all the open vacancies. When submitting the form, the applicant needs to select the vacancy which they want to apply for.

The dropdown with the vacancies is dynamically populated, depending on the open vacancies, using a populate_fields function. When we close a vacancy, it’s automatically removed from the form and vice versa. Which works great… so far so good!

// application form dropdown with vacancies, see https://docs.gravityforms.com/dynamically-populating-drop-down-fields/

add_filter( 'gform_pre_render_3', 'populate_posts' );
add_filter( 'gform_pre_validation_3', 'populate_posts' );
add_filter( 'gform_pre_submission_filter_3', 'populate_posts' );
add_filter( 'gform_admin_pre_render_3', 'populate_posts' );
function populate_posts( $form ) {
 
	foreach ( $form['fields'] as &$field ) {
 
		if ( $field->type != 'select' || strpos( $field->cssClass, 'populate-posts' ) === false ) {
			continue;
		}
 
		// you can add additional parameters here to alter the posts that are retrieved
		// more info: http://codex.wordpress.org/Template_Tags/get_posts
		$posts = get_posts( 'post_type=vacancies&numberposts=-1' );

 
		$choices = array();
 
		foreach ( $posts as $post ) {
			$choices[] = array( 'text' => $post->post_title, 'value' => $post->post_title, 'orderby' => 'title', 'order' => 'ASC' );
		}
 
		// update 'Select a Post' to whatever you'd like the instructive option to be
		$field->placeholder = 'Select a Vacancy...';
		$field->choices = $choices;
 
	}
 
	return $form;
}

But now comes the tricky part :slight_smile:

The vacancy pages have multiple custom fields (using ACF in WordPress).
One of these fields is a contact person which contains an email address.

My goal is to create a hidden field in the application form to select the contact person’s email. This hidden field needs to be autopopulated depending on the vacancy dropdown selector.


Vacancy WP post type: vacancies

Vacancy dropdown admin field: selectedvacancy
Vacancy dropdown custom css class: populate-posts
Fetch custom field from selected vacancy: contact_person_email

Hidden field contact admin field: contact-person-email
Hidden field contact admin custom css class: populate-contact-person-email


Going from here, I would like to add this contact person as a CC recipient, which I luckily already found a solution for: https://docs.gravityforms.com/dynamically-populating-the-post-author/

The form: https://www.i3d.net/career/apply/

Hope this is clear enough?

Thanks for your help in advance!

Jeroen