Pass Field Values to Popup Form

Pass field values from a form embedded on the page into a popup form using Gravity Forms dynamic population.

Instructions

  1. Install this snippet with our free Code Chest plugin. https://gravitywiz.com/gravity-forms-code-chest/

  2. Update feedId with your popup feed ID.

  3. Update the fieldMap array to map page form field IDs to their corresponding dynamic population parameter names configured on the popup form.

Code

Filename: gpp-pass-field-values-to-popup-form.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
 * Gravity Perks // Popups // Pass Field Values to Popup Form
 * https://gravitywiz.com/documentation/gravity-forms-popups/
 *
 * Pass field values from a form embedded on the page into a popup form using
 * Gravity Forms dynamic population.
 *
 * Instructions:
 *
 * 1. Install this snippet with our free Code Chest plugin.
 *    https://gravitywiz.com/gravity-forms-code-chest/
 *
 * 2. Update `feedId` with your popup feed ID.
 *
 * 3. Update the `fieldMap` array to map page form field IDs to their corresponding
 *    dynamic population parameter names configured on the popup form.
 */
( function() {

	var feedId = 123; // Update '123' to the ID of your Popup Feed.

	/**
	 * Map page form field IDs to popup form dynamic population parameter names.
	 *
	 * For compound fields (Name, Address), use the sub-field ID.
	 * e.g. { fieldId: '1_3', param: 'first_name' }
	 */
	var fieldMap = [
		{ fieldId: '1', param: 'parameter_name' },
		{ fieldId: '3', param: 'another_parameter_name' },
		// Add more mappings as needed.
	];

	function getFieldValue( fieldId ) {
		var field = document.getElementById( 'input_' + GFFORMID + '_' + fieldId );

		if ( field && /^(INPUT|SELECT|TEXTAREA)$/.test( field.tagName ) ) {
			// Multi-select: return comma-separated selected values
			if ( field.multiple && field.selectedOptions ) {
				return Array.prototype.map.call( field.selectedOptions, function( opt ) {
					return opt.value;
				} ).join( ',' );
			}

			return field.value;
		}

		// Radio/checkbox — query checked inputs inside the field container
		var container = document.getElementById( 'field_' + GFFORMID + '_' + fieldId );
		if ( ! container ) {
			return '';
		}

		var checkedInputs = container.querySelectorAll( '.gfield-choice-input:checked' );
		if ( checkedInputs.length ) {
			return Array.prototype.map.call( checkedInputs, function( input ) {
				return input.value;
			} ).join( ',' );
		}

		return '';
	}

	window.gform.addFilter( 'gpp_popup_config', function( config ) {
		if ( config.feedId !== feedId ) {
			return config;
		}

		var url = new URL( config.iframeUrl );

		fieldMap.forEach( function( mapping ) {
			var value = getFieldValue( mapping.fieldId );
			if ( value !== '' ) {
				url.searchParams.set( mapping.param, value );
			}
		} );

		config.iframeUrl = url.toString();

		return config;
	} );

} )();

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.