Drupal 7 - How to change multiple drop-downs using AJAX?

时间:2022-09-12 08:30:31

I'm new to Drupal & PHP. I have a similar problem to this unanswered question and I could not find one myself.

我是Drupal和PHP的新手。我对这个悬而未决的问题有类似的问题,我自己也找不到。

I need 3 dependent drop-downs where the user need to select:

我需要3个依赖的下拉菜单,用户需要选择:

  • 1st- The Lecturer.
  • 第一 - 讲师。

  • 2nd- one of his related courses.
  • 他的第二个相关课程。

  • 3rd- one of the course chapters.
  • 第3章课程章节之一。

(the data is extracted from a DB)

(数据从数据库中提取)

As individuals they work fine (when I choose a lecturer the course list get updated, when I choose a course the chapters get updated), but when I choose a lecturer the chapter list won't get updated to reflect the changes to the course drop-down.

作为个人,他们工作正常(当我选择讲师时,课程列表会更新,当我选择课程时,章节会更新),但是当我选择讲师时,章节列表将不会更新以反映课程内容的变化-下。

This is the code I wrote so far:

这是我到目前为止编写的代码:

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();
  }
}

Any help would be most appreciated.

非常感激任何的帮助。

1 个解决方案

#1


2  

You can use Hierarchical Select module to do that. You should also follow this tutorial to get you started.

您可以使用“层次选择”模块来执行此操作。您还应该按照本教程开始。

Hope this helps... Muhammad.

希望这有助于......*。

#1


2  

You can use Hierarchical Select module to do that. You should also follow this tutorial to get you started.

您可以使用“层次选择”模块来执行此操作。您还应该按照本教程开始。

Hope this helps... Muhammad.

希望这有助于......*。