autotextfields.inc

  1. examples
    1. 6 ahah_example/autotextfields.inc

Show/hide textfields based on checkbox clicks.

Functions & methods

NameDescription
ahah_example_autotextfields@file Show/hide textfields based on checkbox clicks.
ahah_example_autotextfields_callback
ahah_example_autotextfields_submitSubmit handler for autotextfields. 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/autotextfields.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Show/hide textfields based on checkbox clicks.
  5. */
  6. function ahah_example_autotextfields(&$form_state) {
  7. $default_first_name = !empty($form_state['values']['ask_first_name']) ? $form_state['values']['ask_first_name'] : FALSE;
  8. $default_last_name = !empty($form_state['values']['ask_last_name']) ? $form_state['values']['ask_last_name'] : FALSE;
  9. $form['ask_first_name'] = array(
  10. '#type' => 'checkbox',
  11. '#title' => t('Ask me my first name'),
  12. '#default_value' => $default_first_name,
  13. '#ahah' => array(
  14. 'path' => 'examples/ahah_example/autotextfields/callback',
  15. 'wrapper' => 'textfield-wrapper',
  16. 'effect' => 'fade',
  17. )
  18. );
  19. $form['ask_last_name'] = array(
  20. '#type' => 'checkbox',
  21. '#title' => t('Ask me my last name'),
  22. '#default_value' => $default_last_name,
  23. '#ahah' => array(
  24. 'path' => 'examples/ahah_example/autotextfields/callback',
  25. 'wrapper' => 'textfield-wrapper',
  26. 'effect' => 'fade',
  27. // 'event' => 'change', // default value: does not need to be set explicitly.
  28. ),
  29. );
  30. $form['textfields'] = array(
  31. '#title' => t("Generated text fields for first and last name"),
  32. '#prefix' => '<div id="textfield-wrapper">',
  33. '#suffix' => '</div>',
  34. '#type' => 'fieldset',
  35. '#description' => t('This is where we put automatically generated textfields'),
  36. );
  37. if (!empty($form_state['values']['ask_first_name'])) {
  38. $form['textfields']['first_name'] = array(
  39. '#type' => 'textfield',
  40. '#title' => t('First Name'),
  41. );
  42. }
  43. if (!empty($form_state['values']['ask_last_name'])) {
  44. $form['textfields']['last_name'] = array(
  45. '#type' => 'textfield',
  46. '#title' => t('Last Name'),
  47. );
  48. }
  49. $form['submit'] = array(
  50. '#type' => 'submit',
  51. '#value' => t('Click Me'),
  52. );
  53. return $form;
  54. }
  55. /**
  56. * Submit handler for autotextfields.
  57. * Gets called even when our select is active, so we use the
  58. * $form_state to determine whether the submit handler should actually do
  59. * anything.
  60. */
  61. function ahah_example_autotextfields_submit($form, &$form_state) {
  62. if (!empty($form_state['ahah_submission'])) {
  63. return;
  64. }
  65. // Continue to handle submit processing.
  66. }
  67. function ahah_example_autotextfields_callback() {
  68. $form = ahah_example_callback_helper();
  69. $textfields = $form['textfields'];
  70. // Remove the prefix/suffix wrapper so we don't double it up.
  71. unset($textfields['#prefix'], $textfields['#suffix']);
  72. // Render the output.
  73. $output = theme('status_messages');
  74. $output .= drupal_render($textfields);
  75. // Final rendering callback.
  76. drupal_json(array('status' => TRUE, 'data' => $output));
  77. exit();
  78. }
Login or register to post comments