You are here

function ahah_example_autotextfields in Examples for Developers 6

@file Show/hide textfields based on checkbox clicks.

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

File

ahah_example/ahah_example_autotextfields.inc, line 7
Show/hide textfields based on checkbox clicks.

Code

function ahah_example_autotextfields(&$form_state) {
  $default_first_name = !empty($form_state['values']['ask_first_name']) ? $form_state['values']['ask_first_name'] : FALSE;
  $default_last_name = !empty($form_state['values']['ask_last_name']) ? $form_state['values']['ask_last_name'] : FALSE;
  $form['ask_first_name'] = array(
    '#type' => 'checkbox',
    '#title' => t('Ask me my first name'),
    '#default_value' => $default_first_name,
    '#ahah' => array(
      'path' => 'examples/ahah_example/autotextfields/callback',
      'wrapper' => 'textfield-wrapper',
      'effect' => 'fade',
    ),
  );
  $form['ask_last_name'] = array(
    '#type' => 'checkbox',
    '#title' => t('Ask me my last name'),
    '#default_value' => $default_last_name,
    '#ahah' => array(
      'path' => 'examples/ahah_example/autotextfields/callback',
      'wrapper' => 'textfield-wrapper',
      'effect' => 'fade',
    ),
  );
  $form['textfields'] = array(
    '#title' => t("Generated text fields for first and last name"),
    '#prefix' => '<div id="textfield-wrapper">',
    '#suffix' => '</div>',
    '#type' => 'fieldset',
    '#description' => t('This is where we put automatically generated textfields'),
  );
  if (!empty($form_state['values']['ask_first_name'])) {
    $form['textfields']['first_name'] = array(
      '#type' => 'textfield',
      '#title' => t('First Name'),
    );
  }
  if (!empty($form_state['values']['ask_last_name'])) {
    $form['textfields']['last_name'] = array(
      '#type' => 'textfield',
      '#title' => t('Last Name'),
    );
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Click Me'),
  );
  return $form;
}