user3640056
user3640056

Reputation: 732

PHP Drop Down On Change

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 '</TD></TR></TABLE>';

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

            echo '</TD></TR></TABLE>';

I want to use "href", as 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 "<table border='1'>
    <tr>
    <th>Firstname</th>
    <th>Lastname</th>
    <th>Phone</th>
    <th>E-mail</th>
    </tr>";

    echo "<tr>";
    echo "<td>" . $result[1]['first_name'] . "</td>";
    echo "<td>" . $result[1]['last_name'] . "</td>";
    echo "<td>" . $result[1]['phone'] . "</td>";
    echo "<td>" . $result[1]['email'] . "</td>";
    echo "</tr>";

     echo "</table>";
}

How can I do this?

Thanks :)

Upvotes: 0

Views: 2414

Answers (3)

Germo
Germo

Reputation: 39

Well if you want the PHP to get something you ned to post something. You either add a submit button or:

echo "<select name=\"course\" id=\"course\" onchange=\"this.form.submit()\">
        <OPTION VALUE=0>Choose
         $options
         </SELECT>";

Then you can utilize if(!empty($_GET['Course'])) { because $_GET[] need the form to be submited.

Upvotes: 1

Hara Prasad
Hara Prasad

Reputation: 722

you can modify this code.

myform.php

<script type="text/javascript">
    $(document).ready(function(){
//Below line will get value of Category and store in id
$("#Category").change(function()
{
var id=$(this).val();
var dataString = 'id='+ id;
$.ajax
({
type: "GET",
url: "Ajax_SubCategory.php",
data: dataString,
cache: false,
success: function(html)
{
//This will get values from Ajax_SubCategory.php and show in Subcategory Select option
$("#SubCategory").html(html);
} 
});
});                         
    });

</script> 
<form>   
<?php 

mysql_connect("localhost","root","") or die("error connect");
mysql_select_db("test") or die(" error database");
echo "<select name='Category' id='Category'>";
echo "<option value='' disabled='' selected='' >--Select Category--</option>"; 
$q6=mysql_query("select DISTINCT Category from category");
while($r6=mysql_fetch_array($q6))
{
echo "<option value='$r6[0]' >$r6[0]</option>"; 
}
echo "</select>";
echo "<select name='SubCategory' id='SubCategory'>";
echo "<option value='' disabled='' selected='' >--Select Sub Category--</option>";  
echo "</select>";
?>
</form>

Ajax_SubCategory.php

<?php
mysql_connect("localhost","root","") or die("error connect");
mysql_select_db("test") or die("error database");

if($_GET['id'])
{
$id=$_GET['id'];
$sql=mysql_query("select SubCategory from category where Category='$id'");
    while($row=mysql_fetch_array($sql))
    {
    $data=$row['SubCategory'];
    echo '<option value="'.$data.'">'.$data.'</option>';
    //echo "<input type='checkbox'  value='".$data."' >".$data.;
    }
}
?>

Upvotes: 1

Dan
Dan

Reputation: 3103

Ok this is a bad approach, for multiple reasons:

  1. This should be done using ajax
  2. Don't use $_SESSION for this

I'll help you out. The first thing you need is jquery. You can find it here: http://jquery.com .

Next take a look at this picture to understand how ajax works: enter image description here

In short, ajax is used to make calls to the server and update the page with the response without reloading. In your case, you will make a call to the server, send course and receive the results.

Once you have jquery set up, you have to write this:

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

Upvotes: 2

Related Questions