autocheckboxes.inc

  1. examples
    1. 6 ahah_example/autocheckboxes.inc

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

Functions & methods

NameDescription
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_callbackCallback for autocheckboxes. Process the form with the number of checkboxes we want to provide.
ahah_example_autocheckboxes_submitSubmit 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.

File

ahah_example/autocheckboxes.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * A Self-configure a form based on a select control.
  5. * Add the number of checkboxes specified in the select.
  6. */
  7. function ahah_example_autocheckboxes(&$form_state) {
  8. $default = !empty($form_state['values']['howmany']) ? $form_state['values']['howmany'] : 1;
  9. $form['howmany'] = array(
  10. '#title' => t('How many checkboxes do you want?'),
  11. '#type' => 'select',
  12. '#options' => array(1=>1, 2=>2, 3=>3, 4=>4),
  13. '#default_value' => $default,
  14. '#ahah' => array(
  15. 'path' => 'examples/ahah_example/autocheckboxes/callback',
  16. 'wrapper' => 'checkboxes',
  17. 'effect' => 'fade',
  18. // 'event' => 'change', // default value: does not need to be set explicitly.
  19. ),
  20. );
  21. $form['checkboxes'] = array(
  22. '#title' => t("Generated Checkboxes"),
  23. '#prefix' => '<div id="checkboxes">',
  24. '#suffix' => '</div>',
  25. '#type' => 'fieldset',
  26. '#description' => t('This is where we get automatically generated checkboxes'),
  27. );
  28. $num_checkboxes = !empty($form_state['values']['howmany']) ? $form_state['values']['howmany'] : 1;
  29. for ($i=1; $i<=$num_checkboxes; $i++) {
  30. $form['checkboxes']["checkbox$i"] = array(
  31. '#type' => 'checkbox',
  32. '#title' => "Checkbox $i",
  33. );
  34. if (isset($form_state['values']["checkbox$i"])) {
  35. $form['checkboxes']["checkbox$i"]['#default_value'] = $form_state['values']["checkbox$i"];
  36. }
  37. }
  38. $form['submit'] = array(
  39. '#type' => 'submit',
  40. '#value' => t('Click Me'),
  41. );
  42. return $form;
  43. }
  44. /**
  45. * Submit handler for autocheckboxes.
  46. * Gets called even when our select is active, so we use the
  47. * $form_state to determine whether the submit handler should actually do
  48. * anything.
  49. */
  50. function ahah_example_autocheckboxes_submit($form, &$form_state) {
  51. if (!empty($form_state['ahah_submission'])) {
  52. return;
  53. }
  54. // Continue to handle submit processing.
  55. }
  56. /**
  57. * Callback for autocheckboxes. Process the form with the number of checkboxes
  58. * we want to provide.
  59. */
  60. function ahah_example_autocheckboxes_callback() {
  61. $form = ahah_example_callback_helper();
  62. $checkboxes = $form['checkboxes'];
  63. // Remove the wrapper so we don't double it up.
  64. unset($checkboxes['#prefix'], $checkboxes['#suffix']);
  65. $output = theme('status_messages');
  66. $output .= drupal_render($checkboxes);
  67. // Final rendering callback.
  68. drupal_json(array('status' => TRUE, 'data' => $output));
  69. exit();
  70. }
Login or register to post comments