Dynamic Options for Dropdown, but not Linked to an Event

bilal.abdeen 12 Apr, 2016
Hi,

I want to display a dropdown select element showing the value of last year (2015) and current year (2016). Where should I put my custom code. If I put it just before "HTML (Render Form)", it does not seem to work.

By the way, I managed to get this to show correctly, if a certain event (the value of another radio button) occurs. However, as I said above, I do NOT want it to be dependent on another event. I want these options to show when the form opens.

Following is the code, which works fine if inserted in an ajax event.

<?php
$period = date('Y'); // 2016
$periods[0] = $period - 1 ; // 2015
$periods[1] = $period ; // 2016

$membership_period = array();
$membership_period[0] = $periods[0];
$membership_period[1] = $periods[1];

echo json_encode($membership_period );
?>

Thanks for your effort and time to help...
Bilal Abdeen
GreyHead 14 Apr, 2016
Hi Bilal,

To use in the form onload event you need to add your data to the $form->data array, please see the Dynamic Data section in this FAQ

Bob
bilal.abdeen 14 Apr, 2016
Bob,

Thank you so much. Earlier, I read the FAQ, which you referred me to. However, I was missing the assignment of values to the 'key' & 'value' columns. For the benefit of anybody who reads this in the future, following is the complete solution.

1. Code to be added in a Custom Code element (just before "HTML (Render Form)")
$period = date('Y'); // 2016
$periods[0] = $period - 2 ; // 2014
$periods[1] = $period - 1 ; // 2015
$periods[2] = $period ; // 2016

$membership_period = array();
$membership_period[0][key] = $membership_period[0][value] = $periods[0];
$membership_period[1][key] = $membership_period[1][value] = $periods[1];
$membership_period[2][key] = $membership_period[2][value] = $periods[2];

if ( !isset($form->data[membership_period]) || !$form->data[membership_period] ) {
$form->data[membership_period] = $membership_period;
}
--------------------
2. In the designer view of the dropdown element:
membership_period -> Edit -> Dynamic Data ->
- Data Path: membership_period
- Value key: value
- Text key: key

Thanks,
Bilal Abdeen
GreyHead 15 Apr, 2016
Hi Bilal,

Well found, in your case the key and value were the same, but often they are not. ChronoForms isn't clever enough to guess they should be the same if only one is provided.

Bob
This topic is locked and no more replies can be posted.