Wim Leers codesnippet for Drupal AHAH
<?php
// Build our new form element.
$form_element = _mymodule_add_something_to_form();
// Build the new form.
$form_state = array('submitted' => FALSE);
$form_build_id = $_POST['form_build_id'];
// Add the new element to the stored form. Without adding the element
// to the form, Drupal is not aware of this new elements existence and
// will not process it. We retreive the cached form, add the element,
// and resave.
$form = form_get_cache($form_build_id, $form_state);
$form['somewhere']['very']['deep'] = $form_element;
form_set_cache($form_build_id, $form, $form_state);
$form += array(
'#post' => $_POST,
'#programmed' => FALSE,
);
// Rebuild the form.
$form = form_builder('mymodule_someform', $form, $form_state);
// Render the new output.
$subform = $form['somewhere']['choice'];
$output = theme('status_messages') . drupal_render($subform);
drupal_json(array('status' => TRUE, 'data' => $output));
?>Source: http://wimleers.com/blog/ahah-helper-module
This is how you can set and send a drupal_set_message() via Drupal AHAH. This code is placed first in the function to return the result in the AHAH call.
<?php
if ($start_time > $stop_time) {
drupal_set_message(t("Start date has to be before Stop date"), "error");
return $output = theme('status_messages');
}
?>I have used it like this
<?php
function cdt_stats_navigate($direction){
// Build our new form element.
$form_element = cdt_stats_calendarmonth_form($form_state, $direction, $_POST['timestamp'], $_POST['project_id'], $_POST['view']);
// Build the new form.
$form_state = array('submitted' => FALSE);
$form_build_id = $_POST['form_build_id'];
$form = form_get_cache($form_build_id, $form_state);
$form['linkpair']['info'] = $form_element;
form_set_cache($form_build_id, $form, $form_state);
$form += array(
'#post' => $_POST,
'#programmed' => FALSE,
);
// Rebuild the form.
$form = form_builder('cdt_stats_calendar_form', $form, $form_state, $_POST['project_id'], $_POST['view']);
// Render the new output.
$subform = $form['linkpair']['info'];
$output = theme('status_messages') . drupal_render($subform);
drupal_json(array('status' => TRUE, 'data' => $output));
}
?>