Update form field dynamically through PHP (gform_pre_submission)

I was looking at https://docs.gravityforms.com/gform_pre_submission/ as a way to update a field value based on a previous field’s value.

Here’s the situation:

I have a field named ‘territory’ which has 4 options (display=value):

  1. USA = 1
  2. Canada = 2
  3. Mexico = 3
  4. Other International = 4

Then, later on the form I have a field named ‘country’, which I can hide for 1-3 and I show if 4 is chosen in territory. I want to be able to automatically fill in the country based on what is selected in territory.

Here’s my code snippet:

    add_action( 'gform_pre_submission_2', 'pre_submission_handler' );
    function pre_submission_handler( $form ) {
      if(rgpost( 'input_1' ) == "1"){
        $_POST['input_14'] = "United States";
      }
      if(rgpost( 'input_1' ) == "2"){
        $_POST['input_14'] = "Canada";
      }
      if(rgpost( 'input_1' ) == "3"){
        $_POST['input_14'] = "Mexico";
      }

    }

Also tried the if statement checking if the value is the numerical version as well (… == 1){.

Country isn’t getting set with this code.

Appreciate the help!

That seems like it should work. Is your form ID actually form 2?

I recommend modifying your code to add a couple logging lines. Try this:

add_action( 'gform_pre_submission_2', 'pre_submission_handler' );
function pre_submission_handler( $form ) {
	GFCommon::log_debug( __METHOD__ . '(): The initial POST => ' . print_r( $_POST, true ) );
	if(rgpost( 'input_1' ) == "1"){
		GFCommon::log_debug( __METHOD__ . '(): Matched United States.' );
		$_POST['input_14'] = "United States";
	}
	if(rgpost( 'input_1' ) == "2"){
		GFCommon::log_debug( __METHOD__ . '(): Matched Canada.' );
		$_POST['input_14'] = "Canada";
	}
	if(rgpost( 'input_1' ) == "3"){
		GFCommon::log_debug( __METHOD__ . '(): Matched Mexico.' );
		$_POST['input_14'] = "Mexico";
	}
	GFCommon::log_debug( __METHOD__ . '(): The modified POST => ' . print_r( $_POST, true ) );
}

Sorry, forgot this part. For that to be useful, you will need to first enable logging:

With the modified code, and with logging enabled, please test the form. Then go back to the Forms > Settings > Logging page, and view the Gravity Forms Core log. Share the link to the log file if you need help figuring out what is going on.

Hey Chris,

This helped and I can see that my field is getting switched out, but I probably should have been clearer about the true goal. My issue is actually that I need these values to be updated for a webhook that I’m trying to process. Based on the log timestamps it looks as though the input_14 is updated to United States before the webhook happens, but even though I’ve mapped that field (country) in the pre_submission it’s not getting handed off to the webhooks.

From the GForm log (note the timestamps):
2020-05-28 21:47:34.169352 - DEBUG --> pre_submission_handler(): Matched United States.
2020-05-28 21:47:34.169373 - DEBUG --> pre_submission_handler(): The modified POST => Array
(
[input_1] => 1
[input_2] => caleb@email.com
[input_4] => CA
[input_5] => FM
[input_6] => (123) 456-7890
[input_7_1] => 123
[input_7_2] => 23
[input_7_3] => SGF
[input_7_4] =>
[input_7_6] => United States
[input_10_1] => Missouri
[input_10_2] => Greene
[is_submit_2] => 1
[gform_submit] => 2
[gform_unique_id] =>
[state_2] => WyJbXSIsIjI0YjVkNTRmYWYwNzZjMDQ5NzI4YmU1YzRhNmI4NTU0Il0=
[gform_target_page_number_2] => 0
[gform_source_page_number_2] => 1
[gform_field_values] =>
[input_14] => United States
)

From the webhooks log:
2020-05-28 21:47:35.609162 - DEBUG --> GF_Webhooks::process_feed(): Sending webhook request to [url-ommitted]?territory=1&country&emailAddress=caleb%40email.com&name=CA&company=FM&phone=%28123%29+456-7890&streetAddress1=123&streetAddress2=23&city=SGF&state=Missouri&county=Greene&ipAddress=75.98.104.178; Array

Any thoughts on how to get that to the webhook before it’s sent?

There are a few fields that aren’t required, but I’d to send them as blank as well, so a similar function will solve that for me as well.

Thanks!