Last updated:
This article shows you how to use the Calculated value form element and JavaScript to build an order form that works like a shopping basket. Users choose a product and as many accessories as they like. At the end, they see an order summary listing every item, its price and the total, and then submit their order.
The example is a bike shop:
Most problems with JavaScript in Formcentric come down to four things. Once you're aware of them, your code will usually work first time.
bike is available in your code as the variable bike. The name is case-sensitive.+, they are joined together rather than added up: "10" + "5" gives "105". Convert values to numbers using parseInt() or parseFloat() before doing any calculations.bike[0]. Use isEmptyList(bike) to check whether anything has been selected at all.
The form has three pages: bike, accessories and order summary. Make sure you use the technical names and option values exactly as shown here, as the script relies on them.
Create a Single choice.
| Setting | Value |
| Label | Which bike would you like to order? |
| Technical name | bike |
Add the following options:
| Label | Value |
| City bike – 649,00 € | citybike |
| Touring bike – 899,00 € | touringbike |
| E-bike – 2.499,00 € | ebike |
Create a Multiple choice so that users can tick as many items as they like.
| Setting | Value |
| Label | Would you like to add any accessories? |
| Technical name | accessories |
Add the following options:
| Label | Value |
| Bike helmet – 59,90 € | helmet |
| Folding lock – 44,95 € | lock |
| Pannier bag – 34,50 € | pannier |
| LED light set – 24,99 € | lights |
| Phone mount – 12,49 € | phonemount |
On this page, create two Calculated values: one for the item list and one for the total. Add a Text paragraph above them with the heading Your order. You'll find the JavaScript for both elements in the next section.
Enable the following options for both calculated values:
Both scripts start with the same item catalogue. This is the heart of the form: it maps each option value to a name and a price in cents.
| Setting | Value |
| Label | Selected items |
| Technical name | itemlist |
| JavaScript | See the code block below |
What the function does
items maps each option value to a name and a price in cents.formatEuro() converts an amount in cents into euros, with a comma as the thousands separator and a full stop for the decimals.isEmptyList(bike) checks whether a bike has been selected. If not, nothing is displayed.JSON.parse(JSON.stringify(accessories || [])) turns the multiple selection into a standard JavaScript array. If no accessories are selected, the result is an empty list.Bike helmet: €59.90 for each selected item.Example result: Touring bike: €899.00; Bike helmet: €59.90; Folding lock: €44.95 |
| Setting | Value |
| Label | Total |
| Technical name | total |
| JavaScript | See the code block below |
What the function does
It uses the same item catalogue but adds up all the prices in cents. Only at the very end is the total converted into euros for display.
Example result: 1,003.85 € |
To add a new product, just follow these two steps:
bell.bell: { name: 'Bike bell', price: 899 },.You can show the VAT included in the price in an additional calculated value. To do this, you work it out from the total.
| Setting | Value |
| Label | VAT included (19 %) |
| Technical name | vat |
| JavaScript | Total script with a modified last line |
return formatEuro(sum);, with the code below.Example result: For a total of €1,003.85, the field shows €160.28. |
To show the net amount, create another field in the same way – for example, with the technical name netamount. The last line is then return formatEuro(sum - Math.round(sum * 19 / 119));. In our example, this gives €843.57.
If the summary stays empty or shows the wrong amounts, the cause is almost always one of the following.
| Problem | Cause | Solution |
| Nothing is displayed |
The technical name in the script doesn't match the form element, e.g. Bike instead of bike |
Compare the technical names, paying attention to upper and lower case |
| The display doesn't update | Recalculate value directly in browser isn't enabled | Enable this option for the calculated value |
| An item is missing from the list or costs €0.00 |
The option value differs from the key in the item catalogue, e.g. Helmet instead of helmet |
Make sure the option value and the key match exactly |
The total looks like 899005990 |
The values were joined together as text rather than added up | Calculate with numbers, e.g. via the item catalogue or parseInt() |
The total has odd decimals, e.g. 1003.8499999 |
The calculation used decimal euro amounts | Store prices in cents and only divide by 100 for display |
| The script doesn't work at all |
The code is outside the curly brackets, or a bracket is missing | Place all code inside calculate() { … } and check your brackets |
Prices in the script contain a comma, e.g. 59,90 |
JavaScript uses a full stop as the decimal separator |
Only use whole cent amounts in the item catalogue, e.g. 5990 |
Once you've set everything up, test the following scenarios. If all the results match, your form is calculating correctly.
| Selection | Expected total |
| No bike | Nothing displayed, or an error message on page 1 |
| City bike, no accessories | €649.00 |
| Touring bike, bike helmet, folding lock | €1,003.85 |
| E-bike with all five accessories | €2,675.83 |
You should also submit a test order and check that the item list and total are included in full in the submitted form data.
Your order form is now ready to use and includes:
You can apply the same approach to any product range: use identifiers as option values, then maintain the names and prices in cents in the item catalogue.
For more examples, see the articles Calculated value and JavaScript: Creating a wall colour calculator and Using JavaScript to validate a newsletter subscription.