Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using exponents in Gravity Forms calculations

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.

like image 775
30hourdays Avatar asked Jun 08 '15 17:06

30hourdays


People also ask

How do you calculate gravity forms?

After activating the Perk, go to Gravity Forms and create a new form with one or more Date or Time fields. Now add a Number field to your form and check the box to enable calculations. Inside the text box, you can add date and time field merge tags into your calculations.

Is gravity forms no longer free?

Gravity Forms PricingBasic License: $59 per year * Elite License: $259 per year * Pro License: $159 per year * * Gravity Forms updates and support are provided only for the duration of an active Gravity Forms subscription.

How do you add conditional logic in gravity form?

Within the fly-out, you should see a toggle labeled Enable Conditional Logic. If you click the toggle on, the options to conditionally display or hide this field will be shown.

How do you customize gravity forms?

To style a Gravity Form, you need to apply some simple CSS to the elements of the form. There are several ways you can add custom CSS to your site but the easiest is to go to Appearance>Customize>Additional CSS and add the code there.


1 Answers

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.

like image 124
Dan Avatar answered Oct 06 '22 01:10

Dan