I created 2 dependent dropdowns in Drupal 7. In the first dropdown you need to choose the device, in order to see its attributes in the second dropdown.
When an option from the first list is selected, the Ajax throbber appears but it doesn’t seem to be working and I don’t get the appropriate options in the second dropdown.
I have read all examples and resources I could find online, but I haven’t found how to fix this. Any help would be very much appreciated.
Below I attach the relevant code. Apologies in advance, I am new in Drupal.
function my_module_getdevicesfromrooms() {
$dropdown1 = array();
$tabdelimited = file_get_contents("http://.../tabdelimited");
// some code
return $dropdown1;
}
/**
* The device name is required in order to get the attributes using this function
*/
function my_module_getattributesfromdevices($test) {
$dropdown2 = array();
$tabdelimited = file_get_contents("http://....");
// some code
return $dropdown2;
}
function my_module_form_ajax($form, &$form_state) {
$form['#prefix'] = "<div id='latest-div'>";
$form['#suffix'] = "</div>";
$options_first = my_module_getdevicesfromrooms();
$value_dropdown_first = isset($form_state['values']['my_module_deviceurn']) ? $form_state['values']['my_module_deviceurn'] : $options_first;
$state2 = my_module_getattributedfromdevices($value_dropdown_first);
$form['my_module_deviceurn'] = array(
'#type' => 'select',
'#title' => 'Device',
'#options' => $options_first,
'#default_value' => $value_dropdown_first,
);
$form['my_module_apply'] = array(
'#type' => 'button',
'#title' => t('Apply'),
'#default_value' => t('apply'),
'#ajax' => array(
'callback' => 'my_module_ajax_callback',
'wrapper' => 'nodes-div',
'method' => 'replace',
'effect' => 'fade',
),
);
$form['my_module_attributes'] = array(
'#type' => 'select',
'#title' => t('Attributes'),
'#options' => $state2,
'#default_value' => variable_get('my_module_attributes', ''),
);
return $form;
}
function my_module_ajax_callback($form, $form_state) {
return $form;
}