Skip to content

Instantly share code, notes, and snippets.

@rfay
Created April 4, 2011 04:21
Show Gist options
  • Select an option

  • Save rfay/901132 to your computer and use it in GitHub Desktop.

Select an option

Save rfay/901132 to your computer and use it in GitHub Desktop.
Hacked to show sequence of events in AJAX
/**
* A very basic form which with an AJAX-enabled submit.
*
* On submit, the markup in the #markup element is updated.
*/
function ajax_example_submit_driven_ajax($form, &$form_state) {
if (empty($form_state['sequence'])) {
$form_state['sequence'] = 0;
}
else {
$form_state['sequence']++;
}
$form['box'] = array(
'#type' => 'markup',
'#prefix' => '<div id="box">',
'#suffix' => '</div>',
'#markup' => '<h1>Initial markup for box</h1>',
);
$form['submit'] = array(
'#type' => 'submit',
'#ajax' => array(
'callback' => 'ajax_example_submit_driven_callback',
'wrapper' => 'box',
'name' => 'submit1',
),
'#value' => t('Submit'),
);
$form['sequence'] = array(
'#type' => 'markup',
'#markup' => '<h3>Sequence on form build is ' . $form_state['sequence'] . '</h3>',
);
return $form;
}
function ajax_example_submit_driven_ajax_submit($form, &$form_state) {
$form_state['sequence']++;
drupal_set_message('Submit handler fired. Sequence = ' . $form_state['sequence']);
}
/**
* Select the 'box' element, change the markup in it, and return it as a
* renderable array.
*
* @return renderable array (the box element)
*/
function ajax_example_submit_driven_callback($form, $form_state) {
// In most cases, it is recomended that you put this logic in form generation
// rather than the callback. Submit driven forms are an exception, because
// you may not want to return the form at all.
$element = $form['box'];
$element['#markup'] = "Clicked submit ({$form_state['values']['op']}): " . date('c');
$form_state['sequence']++;
$element['#markup'] .= " AJAX callback Sequence=" . $form_state['sequence'];
return $element;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment