Skip to main content

Creating Formulas

Learn how to create mathematical formulas using Divi Form Builder's Calculable Fields. Formulas utilize MathExecutor for server-side and JavaScript eval for frontend calculations. Use %%form_field_id%% to reference form field values. For more information, on how to set up the fields and where to add the formulas, please refer to the other documents in this section.

Basic Operators (Server-Side & Frontend)

You can use the following functions and operators in your formulas. To incorporate form field values into your formulas, use the %%form_field_id%% syntax. Replace form_field_id with the actual ID of the form field you want to reference.

  • Addition: +

    %%field_1_id%% + %%field_2_id%%
  • Multiplication: *

    %%field_1_id%% * %%field_2_id%%
  • Division: /

    %%field_1_id%% / %%field_2_id%%
  • Subtraction: -

    %%field_1_id%% - %%field_2_id%%
  • Parentheses: ()

    (%%field_1_id%% + %%field_2_id%%) * 3

Advanced Server-Side Calculation

Server-side calculation provides additional functions that you can use in your formulas:

Average (avg): Calculates the average of the specified values.

avg(x, y, ...)`

Round (round): Rounds the specified value to the given number of decimal places.

round(x, decimalPlaces)`

Server-side Calculation Examples with Functions

  • Average Calculation: Calculate the average quiz score from three form fields:

    avg(%%quiz_score_1%%, %%quiz_score_2%%, %%quiz_score_3%%)
  • Rounded Total: Calculate and round the average amount per installment to two decimal places:

    round(%%total_amount%% / %%installments%%, 2)
  • Addition: Add the quantity to the average of three price values:

    %%quantity%% + avg(%%price_1%%, %%price_2%%, %%price_3%%)
  • Multiplication: Multiply the quantity by the unit price and round the result to two decimal places:

    round(%%quantity%% * %%unit_price%%, 2)
  • Division: Divide the total amount by the number of installments:

    %%total_amount%% / %%installments%%
  • Subtraction: Subtract the discount from the initial value:

    %%initial_value%% - %%discount%%
  • Complex Formula: Calculate the average of 'field_1_id' and -'field_2_id', multiply the result by 3, and round to one decimal place:

    round(avg(%%field_1_id%%, %%field_2_id%%) * 3, 1)

Advanced Frontend Calculation

Frontend calculation offers several advanced functions/utilities however these differ from the server-side options. Frontend calculations in Divi Form Builder use JavaScript's eval() method to process formulas in the browser.

Below are some examples of frontend calculation.

Frontend Calculation Examples

  • Conditional Logic: Performs a calculation based on a condition:

    %%field_1%% > 50 ? %%field_1%% * 2 : %%field_1%% / 2

    Explanation:

    • If field_1 is greater than 50, the result is field_1 * 2.
    • Otherwise, the result is field_1 / 2.
  • Percentage Calculation: Calculates a percentage of a value:

    (%%field_1%% * %%field_2%%) / 100

    If field_1 = 200 and field_2 = 15, the result is 30 (15% of 200).

  • Complex Formula with Multiple Fields: Combines operations across several fields:

    (%%field_1%% + %%field_2%%) * (%%field_3%% - %%field_4%%) / %%field_5%%

    Explanation:

    • Adds field_1 and field_2.
    • Multiplies the result by the difference between field_3 and field_4.
    • Divides the entire result by field_5.
  • Safe Calculation with Fallbacks: Handles cases where fields may be empty or undefined:

    (%%field_1%% || 0) + (%%field_2%% || 0) * ((%%field_3%% || 1) - (%%field_4%% || 0))

    Explanation:

    • Defaults field_1 and field_2 to 0 if they are empty.
    • Defaults field_3 to 1 and field_4 to 0.
  • String-Based Calculation: Concatenates strings or calculates their combined length:

    `${%%field_1%%} ${%%field_2%%}`.length

    Explanation:

    • Concatenates field_1 and field_2 with a space.
    • Returns the total length of the resulting string.
tip

Frontend calculations are performed inside of JavaScript eval. You can experiment with formalus in your browser console by placing them inside eval(). For example, you can run eval(30 > 50 ? 30 * 2 : 30 / 2); and eval(70 > 50 ? 70 * 2 : 70 / 2); in the console to make sure the conditional formula works.