我是 Drupal 和 PHP 的新手。 我有与 this unanswered question 类似的问题我自己也找不到。
我需要 3 个相关的下拉菜单,用户需要在其中进行选择:
- 1st - 讲师。
- 第二 - 他的一门相关类(class)。
- 第三 - 其中之一 类(class)章节。
(数据是从数据库中提取的)
作为个人,他们工作得很好(当我选择讲师时,类(class)列表会更新,当我选择类(class)时,章节会更新),但是当我选择讲师时,章节列表不会更新以反射(reflect)对类(class)下拉菜单。
这是我到目前为止写的代码:
function test_form($form, &$form_state)
{
$form = array();
$lect_options = drupal_map_assoc(get_lecturer_dropdown_options());
$selected_lect = isset($form_state['values']['lect']) ? $form_state['values']['lect'] : key($lect_options);
$form['lect'] = array(
'#type' => 'select',
'#title' => t('Lecturer Name:'),
'#default_value' => $selected_lect,
'#options' => $lect_options,
'#ajax' => array(
'callback' => 'ajax_dependent_crs_dropdown_callback',
'wrapper' => 'dropdown-course-replace',
),
);
$course_options = drupal_map_assoc(_ajax_get_crse_dropdown_options($selected_lect));
$selected_course = isset($form_state['values']['course']) ? $form_state['values']['course'] : key($course_options);
$form['course'] = array(
'#type' => 'select',
'#title' => t('Courses:'),
'#prefix' => '<div id="dropdown-course-replace">',
'#suffix' => '</div>',
'#options' => $course_options,
'#default_value' => $selected_course,
'#ajax' => array(
'callback' => 'ajax_dependent_chap_dropdown_callback',
'event' => 'change',
'wrapper' => 'dropdown-chap-replace',
),
);
$form['chapter'] = array(
'#type' => 'select',
'#title' => t('Chapter:'),
'#prefix' => '<div id="dropdown-chap-replace">',
'#suffix' => '</div>',
'#options' => _ajax_get_chap_dropdown_options($selected_course),
);
return $form;
}
function ajax_dependent_crs_dropdown_callback($form, $form_state) {
return $form['course'];
}
function ajax_dependent_chap_dropdown_callback($form, $form_state) {
return $form['chapter'];
}
function get_lecturer_dropdown_options()
{
$lect_array = array();
$lect_result = db_query("SELECT Vod_Lectuerers.LecturerID, Vod_Lectuerers.LecFirsName, Vod_Lectuerers.LecLastName FROM Vod_Lectuerers ORDER BY Vod_Lectuerers.LecturerID");
foreach ($lect_result as $lect_key=>$lect_row)//Get each lecturer data from the query result
{
$lect_array[] = $lect_row->lecturerid . '# ' . $lect_row->lecfirsname . ' ' . $lect_row->leclastname;
}
return $lect_array;
}
function _ajax_get_crse_dropdown_options($key)
{
if ($key!='')
{
$course_data = array();
$lect_id = substr($key, 0, 4);//cut the ID for the key recived by function
$crs_result = db_query("SELECT Vod_Course.CourseID, Vod_Course.CourseName FROM Vod_Course WHERE Vod_Course.LecturerID='$lect_id' ORDER BY Vod_Course.CourseID", array($lect_id));
foreach ($crs_result as $crs_key=>$crs_row)//Get each course data from the query result
{
$course_data[] = $crs_row->courseid . '# ' . $crs_row->coursename;
}
return $course_data;
}
else
{
return array();
}
}
function _ajax_get_chap_dropdown_options($key)
{
if ($key!='')
{
$chapter_data = array();
$course_id = substr($key, 0, 1);//cut the ID
$cptr_result = db_query("SELECT Vod_Chapters.ChapterID, Vod_Chapters.ChapterDescription FROM Vod_Chapters WHERE Vod_Chapters.CourseID='$course_id' ORDER BY Vod_Chapters.ChapterID", array($course_id));
foreach ($cptr_result as $cptr_key=>$cptr_row)//Get each chapter data from the query result
{
$chapter_data[] = $cptr_row->chapterid . '# ' . $cptr_row->chapterdescription;
}
return $chapter_data;
}
else
{
return array();
}
}
如有任何帮助,我们将不胜感激。
最佳答案
您可以使用 Hierarchical Select module要做到这一点。您还应该关注this tutorial让你开始。
希望这对...穆罕默德有帮助。
关于ajax - Drupal 7 - 如何使用 AJAX 更改多个下拉菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11341399/