home | career | drupal | java | mac | mysql | perl | php | scala | uml | unix

Drupal example source code file (autocheckboxes.inc)

This example Drupal source code file (autocheckboxes.inc) is included in the DevDaily.com "Drupal Source Code Warehouse" project. The intent of this project is to help you "Learn Drupal by Example".

PHP - Drupal tags/keywords

array, checkboxes, default_value, empty, form, form_state, function, if, output, php, suffix, title, type

The autocheckboxes.inc Drupal example source code

<?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',
      // 'event' => 'change', // default value: does not need to be set explicitly.
    ),

  );

  $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.
  print drupal_json(array('status' => TRUE, 'data' => $output));
  exit();
}

Other Drupal examples (source code examples)

Here is a short list of links related to this Drupal autocheckboxes.inc source code file:

new blog posts

"Drupal" is a registered trademark of Dries Buytaert.

my drupal tutorials and examples  

Copyright 1998-2016 Alvin Alexander, alvinalexander.com
All Rights Reserved.

Beginning in 2016, a portion of the proceeds from pages under the '/drupal-code-examples/' URI will be donated to charity.