Populating secondary dropdown (select) contents depending on selected primary dropdown.
Populating secondary dropdown (select) contents depending on selected primary dropdown.
I have a table for student exam results. When a user creates a new entry in the table, I want them to first select a student from a dropdown, and then for a secondary dropdown to become populated with the list of course modules that this particular student has taken.
Currently the modules dropdown is populated with all of the modules available, but I want to restrict this list just to modules that the student has actually taken. In order to do this I assume I would need to repopulate the modules list somehow from an onchange event attached to the students list, but I'm uncertain how to go about this.
if (!isset($_POST['action'])) {
// Get a list of values for the select lists
$data['ModuleCodes'] = $db
->selectDistinct('Modules', 'ModuleID as value, ModuleCode as label')
->fetchAll();
$data['CWOutcomes'] = $db
->sql( 'SELECT ID as value, Outcome as label FROM CourseWorkOutcomes ORDER BY label ASC')
->fetchAll();
$data['CourseParts'] = $db
->selectDistinct('CourseParts', 'PartID as value, PartName as label')
->fetchAll();
$data['ExamBoards'] = $db
->sql("SELECT ID as value, ExamBoard as label FROM ExamBoards WHERE ID = '17' ")
->fetchAll();
$data['Students'] = $db
->sql( "SELECT StudentID as value, (Surname + ', ' + Forename) AS label FROM Student ORDER BY label ASC")
->fetchAll();
}
initComplete: function (settings, json) {
// Populate the site select list with the data available in the
// database on load
editor.field('ExamBoardResults.CWOutcomeID').update(json.CWOutcomes);
editor.field('ExamBoardResults.ModuleID').update(json.ModuleCodes);
editor.field('ExamBoardResults.ExamBoardID').update(json.ExamBoards);
editor.field('ExamBoardResults.StudentID').update(json.Students);
}
Replies
I've made a little progress with this problem with help from this discussion, however I'm still experiencing issues that may be related to either my SQL query or returned JSON array.
Once the user selects a student from the list, the browser alerts the Student ID and passes it to the PHP. Once it has received data from the PHP it alerts the array to the screen, and should also update the Modules list with the array data. While it is alerting the array correctly, I get the following error in Chrome's console log...
..and the modules select list is not populated with the data. Is the array formatted incorrectly or am I missing something else?