I am learning forms and ajax. I am currently trying to keep track of how many times the select value is changed by using a element of type value and checking the $form_state['values'] array and incrementing it if it has a value. The problem is that the first time I change the select my form_state variable does not register that the element has been given a value. When I press the button the second time it starts showing a value. It is like the $forms_state variable is lagging by one. Here is my code:
function ajax_example_autocheckboxes($form, &$form_state) {
drupal_set_message(dprint_r($form_state['values'], TRUE));
$default = !empty($form_state['values']['howmany']) ? $form_state['values']['howmany'] : 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' => $default,
'#ajax' => array(
'callback' => 'ajax_example_autocheckboxes_callback',
'wrapper' => 'checkboxes-div',
'method' => 'replace',
'effect' => 'fade',
),
);
$form['checkboxes_fieldset'] = array(
'#title' => t("Generated Checkboxes"),
// The prefix/suffix provide the div that we're replacing, named by
// #ajax['wrapper'] above.
'#prefix' => '
',