Using exponents in Gravity Forms calculations
Asked Answered
M

1

7

Firstly, I am by no means versed in JavaScript or jQuery. That being said, I'm using Gravity Forms to create a savings calculator that contains exponents. I found THIS thread, but am confused as to how it actually works in calculating an exponential value.

Here's my setup:

I have 3 user-defined fields:

  • Principal amount
  • Interest rate
  • Number of monthly payments

The actual calculations are hidden from the user. There are two formulas calculating at once here. The first is the 'User calculations' working to find a 'Total Interest'. The second is a 'Fixed calculations' with a fixed interest rate of 1.95%, instead of the user-defined field, working towards the same goal. The fixed rate aside, both formulas are identical, and the results of which are incorporated into a final formula free of exponents. Thankfully. Here are the first three 'Number' fields (name & formula) after the user input:

'CALC'<br>
( {Interest rate::2} / 12 )  / 100

'MP exponent'
( 1 + {CALC:8} )  <---This is the formula that requires an exponent equal to the user-defined "Number of monthly payments" field.

'MP numerator'
{CALC:8} * {Principal amount::1} * {MP exponent:10}

The formula goes on and incorporates the MP exponent field multiple times. Given my case here, can I even use the aforementioned script (unedited from its source post):

<script>
gform.addFilter( 'gform_calculation_result', function(result, formulaField, formId, calcObj ){    
    if ( formulaField.field_id == "2" ) {
        var field_five = jQuery('#input_2_5').val();
        result = field_five * 12;
    }
    return result;
});
</script>

...for my calculation? I'm not at all sure how to specify something like this in 4 separate fields... I understand this may not be possible with Gravity Forms - or certainly not simple - so I greatly appreciate any help the community may offer me.

Micra answered 8/6, 2015 at 17:7 Comment(3)
You said "the calculations are hidden from the user." Are Calc, MP exponent, and MP numerator set to "Admin Only" in the advanced tab? If so, you won't be able to access them with javascript (as in your example), so that won't work for you. You could, however, do all the calculations in javascript with only the user-visible fields.Arena
There were several issues with the way I was going about this, actually. You're below snippet to the 'functions.php' file hit the nail on the head. Thanks, mate!Micra
Glad I could help. :)Arena
A
1

The javascript snippet you used above will change what the user sees, but it will not change the saved form data, per say. You instead need to add a PHP filter to the functions.php file of your theme.

add_filter( 'gform_calculation_result', function ( $result, $formula, $field, $form, $entry ) {
    if ( $form['id'] == 1 && $field['id'] == 10 ) {
        $base = 1 + (float) rgar( $entry, '8' ); // '8' should be field ID of Calc
        $exponent = (float) rgar( $entry, '1' ); // '1' should be field ID of Number of monthly payments
        $result   = pow( $base, $exponent );
    }
    return $result;
}, 10, 5 );

One warning: this will not change anything that the user "sees" when filling out the form. It will only change the final calculation after the user hits the submit button.

It would also be cleaner to do all of your calculations in a filter like this rather than using hidden form data.

Arena answered 22/10, 2015 at 5:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.