# Arithmetic with forms

I have a form that will help a user build up a quote for fees to move house. The first page of the form will ask the user to select their nearest location from a drop down list, they will then enter the value of the house they wish to buy into a text field.

What I need to happen on the next page is to display the selected location from the drop down list and also the value they entered for the house, but also to display an estimated fee for purchasing the house.

The Fee is calculated by a series of ranges for each location, for example;

``````Location A
House Price | Fee
Up to 80000 | 100
80000 - 100000 | 200
Over 100000 | 300

Location B
House Price | Fee
Up to 80000 | 150
80000 - 100000 | 250
Over 100000 | 350
``````

I have looked at various Form modules, but none of them do any kind of calculations. So I was wondering if anyone has done anything like this before with forms and calculations?

If you look at some forms modules (like FreeForm, Safecraker), you can have "entry_id" within the "return" parameter value and this entry id will be a segment at the returm page. Now you can use SQL via query module to get the posted / saved values for the further calculations.

Let's take an example with FreeForm:

In the FreeForm form tag you can have return parameter as `return="template_group/template_name/%%entry_id%%"`

and you can fetch the saved values with query module in the template "template_group/template_name" like

``````{exp:query sql="SELECT form_field_1 AS location, form_field_2 AS house_price, form_field_3 AS fee FROM exp_freeform_form_entries_1 WHERE entry_id='{segment_3}'"}

Calculation : {house_price} * {house_price} * {fee}
/***
There are some good add-on for mathmatical calculation. Although, you can do mathmatical calculation within SQL.
***/
{/exp:query}
``````

Note: You need to have form_field_x associated with correct field id. Above example SQL will work with FreeForm v4.0+. For lowser version you can write SQL like:

``````{exp:query sql="SELECT location, house_price, fee FROM exp_freeform_entries WHERE entry_id='{segment_3}'"}
Calculation : {house_price} * {house_price} * {fee}
/***
There are some good add-on for mathmatical calculation. Although, you can do mathmatical calculation within SQL.
***/
{/exp:query}
``````

In the same way, if you are using Safecraker, you can do the same without SQL query and just by writing channel entry tag, passing the parameter entry_id="{segment_3}"