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

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

Hi Adey. Have not heard RTFM in a long time :wink:

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:

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:

// 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.

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.

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:

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.

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.

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:

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!