Click here to Skip to main content
15,884,425 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I have a drop down list called courses. When the user chooses a course, I should show him information about the teacher that gives this course. So, on change I need to get the value selected, and show him the results generated from an sql query.


This is my php code:




$sql= "SELECT id, course_period_id from schedule WHERE STUDENT_ID='$_SESSION[student_id]'";
$result=mysql_query($sql);


$options="";

while ($row=mysql_fetch_array($result)) {

$id=$row["id"];
$course_period_id=$row["course_period_id"];
$course= DBGet(DBQuery("SELECT title FROM course_periods WHERE course_period_id='$course_period_id'"));
$options.="<OPTION VALUE=\"$course[1]['TITLE']\">".$course[1]['TITLE'].'</option>';
}

echo '';

echo "<SELECT>
<OPTION VALUE=0>Choose
$options
</SELECT>";

echo '';





I created a php file "teachers_info.php" with the following code:


if(!empty($_GET['Course']))
{
$sql="SELECT teacher_id FROM course_periods where title= '$course'";
$teacher_id= DBGet(DBQuery($sql));

$result= DBGet(DBQyery(" SELECT first_name, last_name, phone, email FROM staff WHERE staff_id = '$teacher_id[1]['teacher_id']'"));

echo "
";


echo "";
echo "";
echo "";
echo "";
echo "";
echo "";

echo "
Firstname Lastname Phone E-mail
" . $result[1]['first_name'] . "" . $result[1]['last_name'] . "" . $result[1]['phone'] . "" . $result[1]['email'] . "
";
}




?>

I know I need to use jquery and ajax but I have no idea how to do so. I've watched so many tutorials but still I dont understand how can I apply this to my code. Note that the project I'm working on supports jquery and ajax.

XML
I've asked this question and got the following answer:

$(function(){

$("select").on("change", function(){

var value = $(this).value(); //get the selected id
$.get("requesturl.php", {course: value}, function(){
// do something with the response
}, "json");

})

})

The requesturl.php file would look as follows:

$course = $_GET["course"]
if($course){
//execute Database query and store it in $result
echo json_encode($result);
}

So,should I add this to my code:

echo "<script type = 'text/javascript'>function(){

$("select").on("change", function(){

var value = $(this).value(); //get the selected id
$.get("requesturl.php", {course: value}, function(){
// do something with the response
}, "json");

})

})

</script>

I tried this:

echo "<script type = 'text/javascript'>function populate(course)
    {
    alert(test);
    }</script>";

but nothing changed and in debug it didn't even pass through this.

I think I should add a submit button that gets the value of the chosen option (course) and goes to the Teachers_Info.php file to generat the tables cz I really can't understand jquery right now I'm so confused. Any ideas please??





Thanks :)
Posted
Updated 15-Oct-14 0:24am
v2

1 solution

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) {
            // success message
        }
    );
}

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

    // Now, all set and we can get the details of teacher.
    $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 :)
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900