ajax_example_advanced_commands

  1. drupal
    1. 8
    2. 7
Versions
7 – 8 ajax_example_advanced_commands($form, &$form_state)

Form to display the AJAX Commands.

Code

examples/ajax_example/ajax_example_advanced.inc, line 16

<?php
function ajax_example_advanced_commands($form, &$form_state) {
  $form = array();
  $form['intro'] = array(
    '#type' => 'markup', 
    '#markup' => t("<div>Demonstrates how AJAX commands can be used.</div>"),
  );

  // Shows the 'after' command with a callback generating commands.
  $form['after_command_example_fieldset'] = array(
    '#type' => 'fieldset', 
    '#title' => t("This shows the Ajax 'after' command. Click to put something below the div that says 'Something can be inserted after this'"),
  );

  $form['after_command_example_fieldset']['after_command_example'] = array(
    '#value' => t("AJAX 'After': Click to put something after the div"), 
    '#type' => 'submit', 
    '#ajax' => array(
      'callback' => 'ajax_example_advanced_commands_after_callback',
    ), 
    '#suffix' => "<div id='after_div'>Something can be inserted after this</div>
                  <div id='after_status'>'After' Command Status: Unknown</div>",
  );

  // Shows the 'alert' command.
  $form['alert_command_example_fieldset'] = array(
    '#type' => 'fieldset', 
    '#title' => t("Demonstrates the AJAX 'alert' command. Click the button."),
  );
  $form['alert_command_example_fieldset']['alert_command_example'] = array(
    '#value' => t("AJAX 'Alert': Click to alert"), 
    '#type' => 'submit', 
    '#ajax' => array(
      'callback' => 'ajax_example_advanced_commands_alert_callback',
    ),
  );

  // Shows the 'append' command.
  $form['append_command_example_fieldset'] = array(
    '#type' => 'fieldset', 
    '#title' => t("This shows the Ajax 'append' command. Click to put something below the div that says 'Something can be inserted after this'"),
  );

  $form['append_command_example_fieldset']['append_command_example'] = array(
    '#value' => t("AJAX 'Append': Click to append something"), 
    '#type' => 'submit', 
    '#ajax' => array(
      'callback' => 'ajax_example_advanced_commands_append_callback',
    ), 
    '#suffix' => "<div id='append_div'>Something can be appended inside this div... </div>
                  <div id='append_status'>'After' Command Status: Unknown</div>",
  );


  // Shows the 'before' command.
  $form['before_command_example_fieldset'] = array(
    '#type' => 'fieldset', 
    '#title' => t("This shows the Ajax 'before' command."),
  );

  $form['before_command_example_fieldset']['before_command_example'] = array(
    '#value' => t("AJAX 'before': Click to put something before the div"), 
    '#type' => 'submit', 
    '#ajax' => array(
      'callback' => 'ajax_example_advanced_commands_before_callback',
    ), 
    '#suffix' => "<div id='before_div'>Something can be inserted before this</div>
                  <div id='before_status'>'before' Command Status: Unknown</div>",
  );

  // Shows the 'changed' command.
  $form['changed_command_example_fieldset'] = array(
    '#type' => 'fieldset', 
    '#title' => t("Demonstrates the AJAX 'changed' command. If region is 'changed', it is marked with CSS. This example also puts an asterisk by changed content."),
  );

  $form['changed_command_example_fieldset']['changed_command_example'] = array(
    '#title' => t("AJAX changed: If checked, div is marked as changed."), 
    '#type' => 'checkbox', 
    '#default_value' => FALSE, 
    '#ajax' => array(
      'callback' => 'ajax_example_advanced_commands_changed_callback',
    ), 
    '#suffix' => "<div id='changed_div'> <div id='changed_div_mark_this'>This div can be marked as changed or not.</div></div>
                  <div id='changed_status'>Status: Unknown</div>",
  );

  // Shows the AJAX 'css' command.
  $form['css_command_example_fieldset'] = array(
    '#type' => 'fieldset', 
    '#title' => t("Demonstrates the AJAX 'css' command."),
  );

  $form['css_command_example_fieldset']['css_command_example'] = array(
    '#title' => t("AJAX CSS: Choose the color you'd like the '#box' div to be."), 
    '#type' => 'select',
    // '#default_value' => 'green', 
    '#options' => array(
      'green' => 'green',
      'blue' => 'blue',
    ), 
    '#ajax' => array(
      'callback' => 'ajax_example_advanced_commands_css_callback',
    ), 
    '#suffix' => "<div id='css_div' style='height: 50px; width: 50px; border: 1px solid black'> box</div>
                  <div id='css_status'>Status: Unknown</div>",
  );

  // Shows the AJAX 'data' command. But there is no use of this information,
  // as this would require a javascript client to use the data.
  $form['data_command_example_fieldset'] = array(
    '#type' => 'fieldset', 
    '#title' => t("Demonstrates the AJAX 'data' command."),
  );

  $form['data_command_example_fieldset']['data_command_example'] = array(
    '#title' => t("AJAX data: Set a key/value pair on a selector."), 
    '#type' => 'textfield', 
    '#default_value' => 'color=green', 
    '#ajax' => array(
      'callback' => 'ajax_example_advanced_commands_data_callback',
    ), 
    '#suffix' => "<div id='data_div'>This div should have key='time'/value='a time string' attached.</div>
                  <div id='data_status'>Status: Unknown</div>",
  );

  // Shows the AJAX 'html' command.
  $form['html_command_example_fieldset'] = array(
    '#type' => 'fieldset', 
    '#title' => t("Demonstrates the AJAX 'html' command."),
  );

  $form['html_command_example_fieldset']['html_command_example'] = array(
    '#title' => t("AJAX html: Replace the HTML in a selector."), 
    '#type' => 'textfield', 
    '#default_value' => 'new value', 
    '#ajax' => array(
      'callback' => 'ajax_example_advanced_commands_html_callback',
    ), 
    '#suffix' => "<div id='html_div'>Original contents</div>
                  <div id='html_status'>Status: Unknown</div>",
  );

  // Shows the AJAX 'prepend' command.
  $form['prepend_command_example_fieldset'] = array(
    '#type' => 'fieldset', 
    '#title' => t("This shows the AJAX 'prepend' command. Click to put something below the div that says 'Something can be inserted after this'"),
  );

  $form['prepend_command_example_fieldset']['prepend_command_example'] = array(
    '#value' => t("AJAX 'prepend': Click to prepend something"), 
    '#type' => 'submit', 
    '#ajax' => array(
      'callback' => 'ajax_example_advanced_commands_prepend_callback',
    ), 
    '#suffix' => "<div id='prepend_div'>Something can be prepended to this div... </div>
                  <div id='prepend_status'>'After' Command Status: Unknown</div>",
  );

  // Shows the AJAX 'remove' command.
  $form['remove_command_example_fieldset'] = array(
    '#type' => 'fieldset', 
    '#title' => t("Shows the Ajax 'remove' command."),
  );

  $form['remove_command_example_fieldset']['remove_command_example'] = array(
    '#title' => t("AJAX 'remove': Check to remove text. Uncheck to add it back."), 
    '#type' => 'checkbox', 
    '#default_value' => FALSE, 
    '#ajax' => array(
      'callback' => 'ajax_example_advanced_commands_remove_callback',
    ), 
    '#suffix' => "<div id='remove_div'><div id='remove_text'>text to be removed</div></div>
                  <div id='remove_status'>'After' Command Status: Unknown</div>",
  );

  // Show off the AJAX 'restripe' command. Also shows classic AJAX replacement
  // on the "how many rows" processing.
  $form['restripe_command_example_fieldset'] = array(
    '#type' => 'fieldset', 
    '#title' => t("Demonstrates the Ajax 'restripe' command."),
  );

  $form['restripe_command_example_fieldset']['restripe_num_rows'] = array(
    '#type' => 'select', 
    '#default_value' => !empty($form_state['values']['restripe_num_rows']) ? $form_state['values']['restripe_num_rows'] : 1, 
    '#options' => drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)), 
    '#ajax' => array(
      'callback' => 'ajax_example_advanced_commands_restripe_num_rows', 
      'method' => 'replace', 
      'wrapper' => 'restripe_table',
    ),
  );
  $form['restripe_command_example_fieldset']['restripe_restripe'] = array(
    '#type' => 'submit', 
    '#value' => t("Restripe the table"), 
    '#ajax' => array(
      'callback' => 'ajax_example_advanced_commands_restripe_callback',
    ), 
    '#suffix' => "<div id='restripe_div'>
                  <table id='restripe_table' style='border: 1px solid black' >
                  <tr id='table-first'><td>first row</td></tr>
                  </table>
                  </div>
                  <div id='restripe_status'>'Restripe' Command Status: Unknown</div>",
  );

  $form['submit'] = array(
    '#type' => 'submit', 
    '#value' => t('Submit'),
  );

  return $form;
}
?>