You are here

ahah_example_autocheckboxes.inc in Examples for Developers 6

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

File

ahah_example/ahah_example_autocheckboxes.inc
View source
<?php

/**
 * @file
 * A Self-configure a form based on a select control.
 * Add the number of checkboxes specified in the select.
 */
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;
}

/**
 * Submit handler for autocheckboxes.
 * Gets called even when our select is active, so we use the
 * $form_state to determine whether the submit handler should actually do
 * anything.
 */
function ahah_example_autocheckboxes_submit($form, &$form_state) {
  if (!empty($form_state['ahah_submission'])) {
    return;
  }

  // Continue to handle submit processing.
}

/**
 * Callback for autocheckboxes. Process the form with the number of checkboxes
 * we want to provide.
 */
function ahah_example_autocheckboxes_callback() {
  $form = ahah_example_callback_helper();
  $checkboxes = $form['checkboxes'];

  // Remove the wrapper so we don't double it up.
  unset($checkboxes['#prefix'], $checkboxes['#suffix']);
  $output = theme('status_messages');
  $output .= drupal_render($checkboxes);

  // Final rendering callback.
  drupal_json(array(
    'status' => TRUE,
    'data' => $output,
  ));
  exit;
}

Functions

Namesort descending Description
ahah_example_autocheckboxes @file A Self-configure a form based on a select control. Add the number of checkboxes specified in the select.
ahah_example_autocheckboxes_callback Callback for autocheckboxes. Process the form with the number of checkboxes we want to provide.
ahah_example_autocheckboxes_submit Submit handler for autocheckboxes. Gets called even when our select is active, so we use the $form_state to determine whether the submit handler should actually do anything.