I have created a custom form in a custom page in the admin of my Drupal 7 site. On this form I have two sets of radio buttons (cities and sectors). I want it so that when I change the cities radio selection, the sector radio values swap to my new set values.
I have the following code in the main form declaration which initially sets up the form.
$form['module']['cities'] = array(
'#type' => 'radios',
'#title' => t('City'),
'#ajax' => array(
'event' => 'change',
'wrapper' => 'sector-wrapper',
'callback' => 'module_field_city_change_ajax_callback',
'method' => 'replace',
),
'#options' => array("1" => "New York", "2" => "Moscow"),
'#required' => TRUE,
);
$form['module']['sectors'] = array(
'#type' => 'radios',
'#title' => t('Sectors'),
'#prefix' => '<div id="sector-wrapper">',
'#suffix' => '</div>',
'#options' => array("Sport"=>"Sport", "Media"=>"Media"),
'#required' => TRUE,
);
This creates forms. You can see I've added a callback 'module_field_city_change_ajax_callback'. The code for the callback is as follows:
function module_field_city_change_ajax_callback($form, $form_state){
$form['module']['sectors'] = array(
'#type' => 'radios',
'#title' => t('Sectors'),
'#prefix' => '<div id="sector-wrapper">',
'#suffix' => '</div>',
'#options' => array ('Aerospace' => 'Aerospace', 'Art' => 'Art' ),
'#description' => t('Some text to show the div was updated?'),
'#required' => TRUE,
);
return $form['module']['sectors'];
)
It almost works. The old sectors are updated with the new form element apart from one major part - the actual radio buttons (#options). I know the div has updated and the form rendered because it has the text 'Some text to show the div was updated?' injected. However there are just no values to click.
Any ideas?