4
$form['animal'] = array(
'#type' => 'select',
'#title' => t('Animal'),
'#options' => load_animals(),
'#ahah' => array(
'event' => 'change',
'path' => 'path/to/ajax/service',
'method' => 'replace',
'effect' => 'fade',
'wrapper' => 'breed-wrapper',
),
);
...
$form['breed'] = array(
'#type' => 'select',
'#title' => t('Breeds'),
'#options' => array('Select animal to load breed'),
'#prefix' => '<div id="breed-wrapper">',
'#suffix' => '</div>',
);
而且下面是AHAH回調處理
$post = $_POST;
$form_state = array('storage' => NULL, 'submitted' => FALSE);
$form_build_id = $post['form_build_id'];
$form = form_get_cache($form_build_id, $form_state);
$args = $form['#parameters'];
$form_id = array_shift($args);
$form['#redirect'] = FALSE;
$form['#post'] = $post;
$form['#programmed'] = FALSE;
$form_state['post'] = $post;
drupal_process_form($form_id, $form, $form_state);
// New form elements
$breed_form = $form['breed'];
$options = load_breeds((int)$post['animal']);
$breed_form['#options'] = $options;
$form['breed'] = $breed_form;
form_set_cache($form_build_id, $form, $form_state);
$form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
unset($breed_form['#prefix'], $breed_form['#suffix']);
// Render the new output.
$output .= drupal_render($breed_form);
drupal_json(array('status' => TRUE, 'data' => $output));
默認形式提交處理
function default_form_submit(&$form, $form_state){
$clicked_button = $form_state['clicked_button']['#value'];
$values = $form_state['values'];
if($clicked_button == $values['submit']){
unset($values['op'], $values['submit'], $values['form_build_id'],
$values['form_token'], $values['form_id']);
....
drupal_goto($_REQUEST['q'], $query);
}
}
當我終於提交以正常發佈方式形成的有效證明n錯誤報告爲
An illegal choice has been detected.
我是否正確使用form_set_cache()?在AHAH帖子上,默認表單提交處理程序也被調用。由於此處理程序包含重定向邏輯,因此AHAH請求已摺疊。如何繞過它即使我正在做click_button驗證?
我不太確定,但我認爲這篇文章會幫助你很多。 http://www.mindyourcode.com/php/adding-dynamic-form-elements-using-ahah-in-apply-for-for-role-module/ – Mashhadi 2013-03-07 12:46:50