Dynamic dropdown options

Step-by-Step Guide: How to Load Dropdown Options from a Database in CFv6

Overview

To load dropdown options from a database in CFv6, first add a Read Data action in the setup section above the Display Section. Configure it to retrieve key/value pairs from your table, then reference the action's variable in the dropdown's options box. Alternatively, you can use a PHP function that returns an array to populate the dropdown dynamically.

In CFv6 in order to load a dropdown's options from the database, you need to follow the steps below:

  1. Add a Read Data action under the setup section and make sure that its above the Display Section action.
  2. Open the Read Data settings, choose the table you want to load the data from, set the "Select type" to "Key/value pairs array" and in the "Fields to retrieve" add the two field names you want to use for the dropdown options values and labels respectively. Or, if you want to use the same field for both value and label add just that name.
  3. Take a note of the Read Data action name, it's inside a black label, let's assume it is set to "read_data1", go to the dropdown, and write the following in the "options" box:
    {var:read_data1}

Please note that any array can be used to populate a dropdown the same way, as long as the variable called in the options box returns an array, so for example, you can use the following PHP code inside a PHP function instead of using a "Read Data" action:

$options = ["y" => "Yes", "n" => "No"];
return $options;

assuming the PHP function name is "php1", you can use {var:php1} to populate the dropdown options.

Comments:

You need to login to be able to post a comment.