Creating an order form with a basket

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:

  1. On page 1, users choose a bike – for example, the Touring bike at €899.00.
  2. On page 2, they add accessories – for example, a Bike helmet at €59.90 and a Folding lock at €44.95.
  3. On page 3, they review the summary and the total of €1,003.85, and submit their order.

Before you start

Most problems with JavaScript in Formcentric come down to four things. Once you're aware of them, your code will usually work first time.

  1. Form elements are accessed by their technical name
    In JavaScript, you refer to each form element by its technical name. An element with the technical name bike is available in your code as the variable bike. The name is case-sensitive.
  2. Values are always text (strings) Formcentric stores all form values as strings. If you combine two values using +, they are joined together rather than added up: "10" + "5" gives "105". Convert values to numbers using parseInt() or parseFloat() before doing any calculations.
  3. Selections return a list
    Single and multiple selections return a list of the selected option values. For a single selection, the chosen value is the first item in the list, i.e. bike[0]. Use isEmptyList(bike) to check whether anything has been selected at all.
  4. Your code sees the value, not the label
    Every option has a label (what users see) and a value (what your code receives). Your script always works with the value. If you change a value later on, remember to update it in your script too.

Building the form

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.

Page 1: Choosing a bike

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

Page 2: Choosing accessories

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

Page 3: Order summary

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:

  1. Show value in form, so that users can see the result.
  2. Recalculate value directly in browser, so that the display updates straight away.

Adding the JavaScript

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.

Calculated value for the item list

Setting Value
Label Selected items
Technical name itemlist
JavaScript See the code block below

What the function does

  1. items maps each option value to a name and a price in cents.
  2. formatEuro() converts an amount in cents into euros, with a comma as the thousands separator and a full stop for the decimals.
  3. isEmptyList(bike) checks whether a bike has been selected. If not, nothing is displayed.
  4. 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.
  5. The loop builds a line such as Bike helmet: €59.90 for each selected item.
Example result: Touring bike: €899.00; Bike helmet: €59.90; Folding lock: €44.95

Calculated value for the total

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 €

Adding more items

To add a new product, just follow these two steps:

  1. Add a new option to the single or multiple selection – for example, with the value bell.
  2. Add a line to the item catalogue in both scripts – for example, bell: { name: 'Bike bell', price: 899 },.

Showing VAT

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
  1. On page 3, create a new Calculated value using the settings in the table.
  2. Copy the complete total script into the field.
  3. Replace the last line, return formatEuro(sum);, with the code below.
  4. Enable Show value in form and Recalculate value directly in browser.

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.

Troubleshooting

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

Testing

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.

Summary

Your order form is now ready to use and includes:

  1. product selection via a single selection
  2. accessory selection via a multiple selection, with any number of items
  3. an automatically generated item list with individual prices
  4. a total free from rounding errors
  5. a breakdown of the VAT included

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.

Feedback