# Calculation / task outside form via function. (Passing field data back and forth)

**URL:** https://community.gravityforms.com/t/calculation-task-outside-form-via-function-passing-field-data-back-and-forth/4661
**Category:** Get Help
**Tags:** custom, gform\_pre\_submission, gform\_calculation\_result, populate-anything
**Created:** [May 1, 2020, 9:29am UTC](https://community.gravityforms.com/t/calculation-task-outside-form-via-function-passing-field-data-back-and-forth/4661 "2020-05-01T09:29:58Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Adey](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/adey/32/4126_2.png) [@Adey](https://community.gravityforms.com/u/Adey)
#### Post date: [May 1, 2020, 9:29am UTC](https://community.gravityforms.com/t/calculation-task-outside-form-via-function-passing-field-data-back-and-forth/4661/1 "2020-05-01T09:29:58Z")

</div>

I wondered if the following is possible.

Form fields.

number field ID 1  
number field ID 2  
number field ID 3

have number field 1 and 2 get passed into a function and have the result returned and put into number field 3. I fully appreciate this is what the advanced calculations is for but this would demonstrate to me.

A. How to pass data from a field into a function  
B. How to return a value back to a form.  
C. How to do this in real time.

Knowing the above I’m guessing is a pretty core concept and can be adapted for a number of scenarios. Be it numbers of any manner of data.

Note this would need to be real time as in if the user changes fields 1 or 2 then 3 changes dynamically.

Once again thanks for time. I have the actual form creation on the front end in my head but the front to back end needs clarification to me thanks. Yes I know RTFM but @chrishajer has helped me numerous time with great success (thanks Chris).

Regards

Adey

---

<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 1, 2020, 1:06pm UTC](https://community.gravityforms.com/t/calculation-task-outside-form-via-function-passing-field-data-back-and-forth/4661/2 "2020-05-01T13:06:40Z")

</div>

Hi Adey. Have not heard RTFM in a long time 😉

To take a couple numbers (or anything submitted in the form) and do some calculations (or string manipulation), and place the result in another field in the form (the submission/entry) you can use the gform\_pre\_submission filter:

> **[gform\_pre\_submission - Gravity Forms Documentation](https://docs.gravityforms.com/gform_pre_submission/#2-populate-a-field-using-the-value-from-another-field)**
>
> This action can be used to modify the posted values after form validation but prior to creating the entry.

You would read what is submitted in the \_POST using that filter, perform your calculations on those numbers, and inject that back into the \_POST. Here’s a simple example that will take the values submitted in field 1 and field 2, multiply them, and store them in field 3 before the entry is created:

```auto
// restrict to form 28 only
add_action( 'gform_pre_submission_28', 'make_some_noise' );
function make_some_noise( $form ) {
    // multiple field 1 by field 2 and store it in field 3
    $result = intval(rgpost( 'input_1' ) ) * intval( rgpost( 'input_2' ) );
    $_POST['input_3'] = $result;
}

```

Let me know if you have any questions about how that works.

---

<div class="post-metadata">

### Author: ![Adey](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/adey/32/4126_2.png) [@Adey](https://community.gravityforms.com/u/Adey)
#### Post date: [May 1, 2020, 4:54pm UTC](https://community.gravityforms.com/t/calculation-task-outside-form-via-function-passing-field-data-back-and-forth/4661/3 "2020-05-01T16:54:22Z")

</div>

Thanks @chrishajer

I had managed to do the pre render form submission style calculations. I was referring more to a real time calculation when the user is filling the form (If this is actually possible via a function).

Once I know how to do this via a function it will open up a world of possibilities for me. I can then use this principle for more than number manipulation.

Again I appreciate your time and patience.

---

<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 1, 2020, 6:24pm UTC](https://community.gravityforms.com/t/calculation-task-outside-form-via-function-passing-field-data-back-and-forth/4661/4 "2020-05-01T18:24:28Z")

</div>

Oops: missed the mark!

For realtime calculations in the form, I recommend using the number field with the calculation enabled. That gives you access to simple calculations (addition, subtraction, multiplication, division, grouping with parenthesis). If you need more than that, you can use the gform\_calculation\_result filter:

> **[gform\_calculation\_result - Gravity Forms Documentation](https://docs.gravityforms.com/gform_calculation_result/)**
>
> This filter can be used to modify the result of a number field calculation or calculated product field.

There is a jQuery version, which is used to manipulate the calculated field on-screen, and you also need to use the PHP version, to validate the calculated field when the form is submitted.

---

<div class="post-metadata">

### Author: ![Adey](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/adey/32/4126_2.png) [@Adey](https://community.gravityforms.com/u/Adey)
#### Post date: [May 1, 2020, 6:55pm UTC](https://community.gravityforms.com/t/calculation-task-outside-form-via-function-passing-field-data-back-and-forth/4661/5 "2020-05-01T18:55:10Z")

</div>

Ok can I put this another way @chrishajer.

On a different tangent and I apologise for moving sideways on this. Lets us say wished to take two text fields. concatenate them in a function and pass them back to another field is this possible?.

I’m trying to ask if it is possible to have the form react in real-time and have a function do heavy lifting php tasks. Be it figures, text or otherwise. Then pass it back to a live form field

I have updated the title to reflect the premise.

---

<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 1, 2020, 9:09pm UTC](https://community.gravityforms.com/t/calculation-task-outside-form-via-function-passing-field-data-back-and-forth/4661/6 "2020-05-01T21:09:05Z")

</div>

To do that live in the same page would require JavaScript or jQuery. There’s nothing specific in Gravity Forms that will do that.

If you have a multi page form, you can use PHP and the gform\_pre\_render filter as shown here:

> **[gform\_pre\_render - Gravity Forms Documentation](https://docs.gravityforms.com/gform_pre_render/#3-populate-field-with-values-from-earlier-page)**
>
> The gform\_pre\_render filter is executed before the form is displayed and can be used to manipulate the Form Object prior to rendering the form.

Is that more along the lines of what you’re looking for?

There’s a plugin that can do this in the same page, but that makes it too easy!

> **[Gravity Forms Populate Anything - Gravity Wiz](https://gravitywiz.com/documentation/gravity-forms-populate-anything/)**
>
> Dynamically filter and populate field choices and values with posts, users, taxonomies, terms, Gravity Forms entries, and databases. Pretty much anything!

---

<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 5, 2022, 8:47am UTC](https://community.gravityforms.com/t/calculation-task-outside-form-via-function-passing-field-data-back-and-forth/4661/7 "2022-01-05T08:47:55Z")

</div>


