function ajax_example_autocheckboxes in Examples for Developers 7
Form manipulation through AJAX.
AJAX-enabled select element causes replacement of a set of checkboxes based on the selection.
Related topics
1 string reference to 'ajax_example_autocheckboxes'
- ajax_example_menu in ajax_example/
ajax_example.module - Implements hook_menu().
File
- ajax_example/
ajax_example.module, line 367 - AJAX Examples module file with basic examples.
Code
function ajax_example_autocheckboxes($form, &$form_state) {
// Since the form builder is called after every AJAX request, we rebuild
// the form based on $form_state.
$num_checkboxes = !empty($form_state['values']['howmany_select']) ? $form_state['values']['howmany_select'] : 1;
$form['howmany_select'] = array(
'#title' => t('How many checkboxes do you want?'),
'#type' => 'select',
'#options' => array(
1 => 1,
2 => 2,
3 => 3,
4 => 4,
),
'#default_value' => $num_checkboxes,
'#ajax' => array(
'callback' => 'ajax_example_autocheckboxes_callback',
'wrapper' => 'checkboxes-div',
// 'method' defaults to replaceWith, but valid values also include
// append, prepend, before and after.
// 'method' => 'replaceWith',
// 'effect' defaults to none. Other valid values are 'fade' and 'slide'.
// See ajax_example_autotextfields for an example of 'fade'.
'effect' => 'slide',
// 'speed' defaults to 'slow'. You can also use 'fast'
// or a number of milliseconds for the animation to last.
// 'speed' => 'slow',
// Don't show any throbber...
'progress' => array(
'type' => 'none',
),
),
);
$form['checkboxes_fieldset'] = array(
'#title' => t("Generated Checkboxes"),
// The prefix/suffix provide the div that we're replacing, named by
// #ajax['wrapper'] above.
'#prefix' => '<div id="checkboxes-div">',
'#suffix' => '</div>',
'#type' => 'fieldset',
'#description' => t('This is where we get automatically generated checkboxes'),
);
for ($i = 1; $i <= $num_checkboxes; $i++) {
$form['checkboxes_fieldset']["checkbox{$i}"] = array(
'#type' => 'checkbox',
'#title' => "Checkbox {$i}",
);
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}