Forums

populate dropdown with variables

a5131mae 31 Jan, 2016
Hi Bob,
When loading the form, I would like to populate a dropdown with predfined values - from a list of variables rather than from a database table.
This seems to be very basic, but I can't find a solution for this.

When I put in the dropdown General/Options:
0=$form->data['Opt1']
1=$form->data['Opt2']

and in custom code before rendering the form:

$form->data['Opt1']='O1';
$form->data['Opt2']='O2';

the dropdown displays $form->data['Opt1'] instead of the value.

Could you please give me a hint?
Thanks in advance
ivo
GreyHead 31 Jan, 2016
Hi ivo,

You can use the Dynamic Data tab to do that - but your data needs to be in an array that includes both value and text entries for each option like this:
. . .
$form->data['options'] = array(
  [0] => array('value' => 'aaa', 'text' => 'xxx'),
  [1] => array('value' => 'bbb', 'text' => 'yyy'),
  . . .
)
The names don't need to be 'text' and 'value' - it's the structure that is important.

What is the source of your Opt1, Opt2 data?

Bob
a5131mae 31 Jan, 2016
Dear Bob,
thank you for pointing me to this. I thought that the Dynamic option can only be used with database-derived data.
And AFTER reading your answer and understanding the array-topic I also understand the FAQ "How do I build a select drop-down, radio button or checkbox group?"🙂
However, just for the next user who comes upon this thread: [0] must be '[0]'.

The source of my Dropdown-data should come from a custom code element, mainly because I use "Locales" to implement another language into the form, but consequently, also the text in the dropdown should display in the language of the form.

Thanks again for your help!
ivo
This topic is locked and no more replies can be posted.