Buy Now
Sign in

FAQ search

How can I get useful data from select drop-downs, checkboxes and radio buttons?

Published: Wednesday, 10 September 2014 15:33
All three of these form elements: select drop-downs, checkbox groups and radio button groups can be set up with options lists of value + text. When the form is submitted the value is returned which is not always useful in an email. Also checkbox groups and select drop-downs with the 'multiple' option set return arrays of values. This FAQ shows you how to solve these two problems.
It is common to have lists of options in forms, sometimes entered by hand, sometimes created from a database table that use value + text pairs like this. These might be color options for an item of furniture for example:
This clearly displays the list of colours to the form user but, when the form is submitted, we get either a single value e.g. '2', or an array of values like array('2', '4') if multiple selection is allowed.
These values are fine for the computer to use - and they may be what wew need to save to record the item correctly; but they are not human-friendly. We would prefer to see 'blue' or 'blue, white.
There are a couple of ways to handle this problem. 
If the results are only going to be used by people then it may be enough to change the options list so that the value and text are both the same:
Then the form will return the text value e.g. 'blue' or a text array e.g. array('blue', 'white')
If we need to keep the coded values - as we often do if they are part numbers used by the computer for something later then we need to 'look up' the text values for human use after the form is submitted.
The code to do this is fairly simple. 
First a version to work with radio-button groups and single select drop-down (it assumes that the form input has the name 'color'):
$color_array = array(
  1 => 'green',
  2 => 'blue',
  3 => 'red',
  4 => 'white'
$form->data['color_name'] = $color_array[$form->data['color']];
This creates a color names like blue in the form data so that we can use {color_name} in an email to show the human-readable value, while still saving the coded value to the database.
The second example is for checkbox groups and multiple select drop-downs that return an array of values (it assumes that the form input has the name 'colors'):
$color_array = array(
  1 => 'green',
  2 => 'blue',
  3 => 'red',
  4 => 'white'
$color_names = array();
foreach ( $form->data['colors'] as $v ) {
  $color_names[] = $color_array[$v];
$form->data['color_names'] = implode(', ', $color_names);
This creates a comma separated list of color names like blue, white in the form data so that we can use {color_names} in an email to show the human-readable list, while still saving the coded values to the database.
You can adapt these examples to work with most kinds of coded option lists.

Handling Arrays

ChronoForms has a Handle Arrays action in the Utilities action group (the Core actions group in CFv5) that is intended to take the arrays returned by checkbox groups and by multiple select drop-downs and convert them into lists of values that are can be saved to the database or used in an email.
Normally it is enough to drag the Handle Arrays action into the On Submit event and move it up before the DB Save and/or Email action. 
If you use the Custom Code action above then that should come before the Handle Arrays action as is is designed to work with array values.