You are here

function ahah_example_autocheckboxes in Examples for Developers 6

@file A Self-configure a form based on a select control. Add the number of checkboxes specified in the select.

1 string reference to 'ahah_example_autocheckboxes'
ahah_example_menu in ahah_example/ahah_example.module
Implement hook_menu().

File

ahah_example/ahah_example_autocheckboxes.inc, line 8
A Self-configure a form based on a select control. Add the number of checkboxes specified in the select.

Code

function ahah_example_autocheckboxes(&$form_state) {
  $default = !empty($form_state['values']['howmany']) ? $form_state['values']['howmany'] : 1;
  $form['howmany'] = array(
    '#title' => t('How many checkboxes do you want?'),
    '#type' => 'select',
    '#options' => array(
      1 => 1,
      2 => 2,
      3 => 3,
      4 => 4,
    ),
    '#default_value' => $default,
    '#ahah' => array(
      'path' => 'examples/ahah_example/autocheckboxes/callback',
      'wrapper' => 'checkboxes',
      'effect' => 'fade',
    ),
  );
  $form['checkboxes'] = array(
    '#title' => t("Generated Checkboxes"),
    '#prefix' => '<div id="checkboxes">',
    '#suffix' => '</div>',
    '#type' => 'fieldset',
    '#description' => t('This is where we get automatically generated checkboxes'),
  );
  $num_checkboxes = !empty($form_state['values']['howmany']) ? $form_state['values']['howmany'] : 1;
  for ($i = 1; $i <= $num_checkboxes; $i++) {
    $form['checkboxes']["checkbox{$i}"] = array(
      '#type' => 'checkbox',
      '#title' => "Checkbox {$i}",
    );
    if (isset($form_state['values']["checkbox{$i}"])) {
      $form['checkboxes']["checkbox{$i}"]['#default_value'] = $form_state['values']["checkbox{$i}"];
    }
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Click Me'),
  );
  return $form;
}