Dynamically add notification email via hidden field w/ JS & PHP? [RESOLVED]

I’m currently building a page with a drop-down menu containing a list of contacts, and upon selecting a contact, I’d like to have a single Gravity contact form populated with that email address for sending a notification (rather than creating numerous separate forms, each hard-coded to notify a given email address.) My hope is that I can populate a hidden form field via jQuery w/ the contact email (pulled from the select menu value), and then instruct Gravity forms to send a form notification w/ form data to the selected email address. From reviewing the docs and other threads here, it appears I can use the gform_pre_submission_filter in a way like the following (?)

add_filter('gform_pre_submission_filter', 'add_email_notify');
function add_email_notify( $form ) {
    $form['fields'][ID] = $form['notifications']['routing']['email']; // reference ID of hidden field w/ JS-populated email address
    // return the modified form object
    return $form;
}

The ID would reference the ID of the hidden form field-- is this approach correct? I’ve also seen suggestions related to creating a hidden field w/ parameter name and referencing that parameter to get a field populated with the email address, but I’m not sure of the best route. Thanks for any insight here!

Alternatively, you can simply setup a notification in the forms settings and send the form data to the email address by using conditional logic.

Thanks @user5c0166073a6c48.6 - I would like to be able to dynamically populate the notification email address if possible; is what I’ve detailed above a viable approach?

I did some digging for you. I believe this is the correct snippet you need.

// Change 1 on the following to your form id number.
add_action( 'gform_pre_submission_1', 'pre_submission_handler' );
function pre_submission_handler( $form ) {
    // Change 14 and 5 to the id number of your fields.
    $_POST['input_14'] = rgpost( 'input_5' );
}

Populate a field using the value from another field

This example changes the post variable for field 14 to the value of field 5 for a form with id 1:

Now you can dynamically add the value of the contact which is the email into an email field and then simply add the email field as the send to in the notifications tab.

This action hook is executed after form validation, but before any notifications are sent and the entry is stored. This action can be used to modify the posted values prior to creating the entry.

Thanks for the response- after spending more time with the documentation, it looks like the gform_notification hook probably makes more sense in this case? I’m assuming the following should work?

// GFORM_NOTIFICATION
add_filter( 'gform_notification', 'append_email', 10, 3 );
function append_email( $notification, $form, $entry ) {
    
	// get email address from hidden field w/ ID of 5
	$contact_email = $entry[5]; 
	
	// append email address to notifications objects
    $notification['to'] .= ', ' . $contact_email;
 
    return $notification;
}

Are you trying any of the snippets or are you looking for confirmation before you do?

You should take a backup but Wordpress default editor will rollback changes if the code you add isn’t correct.

Sorry, haven’t tried yet- I’m preparing ahead of this project since I haven’t really worked w/ Gravity Forms in this way before; I’ll report back on what works and/or doesn’t work- thanks!

Just to report back here for anyone who might need to implement something similar: the gform_notification filter function I detailed above looks to work swimmingly in sending a notification to a dynamically populated email field.

1 Like

Thanks for the update Nick.

© 2008 - 2019. Gravity Forms is a project by Rocketgenius Inc.