Hello,
Let's try the below workout for fixing your issue.
1) First, you are getting all the course details, the student acquired.
$sql = "SELECT S.id, S.course_period_id, CP.title
FROM schedule AS S
LEFT JOIN course_periods AS CP
ON S.course_period_id = CP.course_period_id
WHERE STUDENT_ID = '$_SESSION[student_id]'";
$result = mysql_query($sql);
You got the resultant array now.
2) Need to loop into it, to get the data
echo '<select onchange="javascript: showTeacherInfo();" id="sel_courseList">';
echo '<option value="0">Choose Course</option>';
while ($row = mysql_fetch_array($result)) {
echo '<option value="' . $row['title'] . '">' . $row['title'] . '</option>';
}
echo '</select>';
Drop down is ready now.
3) Now, time to write javascript method, where we will do the AJAX call to get teacher's info.
function showTeacherInfo() {
var courseTitle = $('#sel_courseList :selected').val();
var url = 'teachers_info.php';
$.post(url,
{
courseTitle: courseTitle
},
function(data) {
}
);
}
We are done with the AJAX call.
4) Let's go to teachers_info.php file.
if(isset($_POST['courseTitle']) && !empty($_POST['courseTitle'])) {
$courseTitle = $_POST['courseTitle'];
$sql = "SELECT S.first_name, S.last_name, S.phone, S.email
FROM course_periods AS CP
INNER JOIN staff AS S
ON CP.teacher_id = S.staff_id
WHERE CP.title= '$courseTitle'";
$result = mysql_query($sql);
$teacher_details = mysql_fetch_row($result);
$teacher_first_name = $teacher_details[0];
$teacher_last_name = $teacher_details[1];
$teacher_phone = $teacher_details[2];
$teacher_email = $teacher_details[3];
}
Hopefully, the above code will work for you. Though we have many ways and can say better way to fix your problem, I have chosen the simplest way.
Please let me know, if you are still finding any issue. Thanks and all the best :)