Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8188455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:55:06+00:00 2026-06-07T02:55:06+00:00

I’m new to Drupal & PHP. I have a similar problem to this unanswered

  • 0

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

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

  • 1st- The Lecturer.
  • 2nd- one of his related courses.
  • 3rd- one of
    the course chapters.

(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 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T02:55:07+00:00Added an answer on June 7, 2026 at 2:55 am

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

    Hope this helps… Muhammad.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.