# Update form field dynamically through PHP (gform\_pre\_submission)

**URL:** <https://community.gravityforms.com/t/update-form-field-dynamically-through-php-gform-pre-submission/4911>\
**Category:** Get Help\
**Tags:** rgpost, custom, gform\_pre\_submission\
**Created:** [May 26, 2020, 7:19pm UTC](https://community.gravityforms.com/t/update-form-field-dynamically-through-php-gform-pre-submission/4911 "2020-05-26T19:19:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![frankmaven](https://avatars.discourse-cdn.com/v4/letter/f/d9b06d/32.png) [@frankmaven](https://community.gravityforms.com/u/frankmaven)\
**Post date:** [May 26, 2020, 7:19pm UTC](https://community.gravityforms.com/t/update-form-field-dynamically-through-php-gform-pre-submission/4911/1 "2020-05-26T19:19:58Z")

</div>

I was looking at [https://docs.gravityforms.com/gform\_pre\_submission/](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!

---

<div class="post-metadata">

**Author:** ![chrishajer](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/chrishajer/32/88_2.png) [@chrishajer](https://community.gravityforms.com/u/chrishajer)\
**Post date:** [May 26, 2020, 10:24pm UTC](https://community.gravityforms.com/t/update-form-field-dynamically-through-php-gform-pre-submission/4911/2 "2020-05-26T22:24:36Z")

</div>

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:

```auto
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 ) );
}

```

---

<div class="post-metadata">

**Author:** ![chrishajer](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/chrishajer/32/88_2.png) [@chrishajer](https://community.gravityforms.com/u/chrishajer)\
**Post date:** [May 26, 2020, 10:26pm UTC](https://community.gravityforms.com/t/update-form-field-dynamically-through-php-gform-pre-submission/4911/3 "2020-05-26T22:26:30Z")

</div>

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

> **[Logging and Debugging - Gravity Forms Documentation](https://docs.gravityforms.com/logging-and-debugging/)**
>
> For quite a while, enabling logging within Gravity Forms has been a go-to method for debugging any issues within Gravity Forms. As of Gravity Forms version 2.2, logging functionality has been moved from a separate add-on to becoming a core feature of...

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.

---

<div class="post-metadata">

**Author:** ![frankmaven](https://avatars.discourse-cdn.com/v4/letter/f/d9b06d/32.png) [@frankmaven](https://community.gravityforms.com/u/frankmaven)\
**Post date:** [May 28, 2020, 9:57pm UTC](https://community.gravityforms.com/t/update-form-field-dynamically-through-php-gform-pre-submission/4911/4 "2020-05-28T21:57:41Z")

</div>

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](mailto: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!

---

<div class="post-metadata">

**Author:** ![richardw8k](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/richardw8k/32/2354_2.png) [@richardw8k](https://community.gravityforms.com/u/richardw8k)\
**Post date:** [January 4, 2022, 6:04pm UTC](https://community.gravityforms.com/t/update-form-field-dynamically-through-php-gform-pre-submission/4911/5 "2022-01-04T18:04:38Z")

</div>


