0

我有一个名为“课程”的下拉列表,其中包含选项(math101、eng102 .. 等),我想放置另一个名为(学生姓名)的下拉列表,但这个应该被隐藏,所以当用户从课程中选择一个值时list ,学生姓名的列表现在将显示只参加过这门课程的学生姓名,因此用户可以选择一个..当然所有数据都将从数据库中获取

到目前为止我的代码是

<?php

   include('../connect.php');
   $id=$_SESSION['login_user'];



      $sql = "SELECT CourseName from Course ";
         $result = mysql_query ($sql, $connection);

         echo "<tr><th>Course Name </th>";
         echo "<td><select id='CourseName' name='v1' >";
        while( $row = mysql_fetch_array($result))
   {
        echo "<option value='$row[CourseName]' selected='selected'>$row[CourseName]</option> ";

   }
     echo "</select>";
     echo "</td>";
     echo "</tr>" ;


  $sql = "SELECT StudentName from Student ";
         $result = mysql_query ($sql, $connection);

         echo "<tr><th>Student Name </th>";
         echo "<td><select id='StudentName' name='v2' >";
        while( $row = mysql_fetch_array($result))
   {
        echo "<option value='$row[StudentName]' selected='selected'>$row[StudentName]</option> ";

   }
     echo "</select>";
     echo "</td>";
     echo "</tr>" ;




     echo "</table>" ;
     echo "</font>" ;

    ?>

我的两张桌子是

课程: CourseName var(30) CourseID int(7)

学生:StudentName var(40) 学生 ID int(7) CourseID int(7)

所以我的问题是,如何使“学生姓名”成为隐藏下拉列表取决于“课程”列表,所以当用户选择一门课程时,会出现所有参加该课程的学生姓名(按课程 ID)?

4

2 回答 2

0

如果您不想使用我提供的 Ajax/jQuery 答案,这是另一种方法。选择课程后,它将使用StudentName基于CourseID. 这是通过向选择添加 1 个 javascript 代码onchange='this.form.submit();'CourseID然后isset($_POST['v1'])在选择周围添加来完成的StudentName

<?php

include('../connect.php');
$id=$_SESSION['login_user'];

$sql = "SELECT CourseName from Course ";
$result = mysql_query ($sql, $connection);

  echo "<form action='' method='post'>";
  echo "<table>";
  echo "<tr><th>Course Name </th>";
  echo "<td><select id='CourseName' name='v1' onchange='this.form.submit();'>";  // add onchange function to submit
    echo "<option value=''>Select Course</option> ";  // empty value option
    while( $row = mysql_fetch_array($result)){
    $sel = ($_POST['v1'] == $row[CourseName])? "selected='selected'":"";   // adds selected='selected' if post course same as this course
    echo "<option value='$row[CourseName]'$sel>$row[CourseName]</option> ";
    }
 echo "</select>";
 echo "</td>";
 echo "</tr>";

if ((isset($_POST['v1'])) && ($_POST['v1'] != "")){ // checks if a course is selected
 $sql = "SELECT StudentName from Student WHERE CourseID = ".mysql_real_escape_string($_POST['v1']);  // only selects StudentName which has CourseID
     $result = mysql_query ($sql, $connection);

     echo "<tr><th>Student Name </th>";
     echo "<td><select id='StudentName' name='v2' >";
    while( $row = mysql_fetch_array($result)){
      echo "<option value='$row[StudentName]' selected='selected'>$row[StudentName]</option> ";}
}
 echo "</select>";
 echo "</td>";
 echo "</tr>" ;

 echo "</table>" ;
 echo "</form>" ;

?>
于 2012-11-13T06:43:56.793 回答
0

我这样做的方法是使用 javascript/jQuery/Ajax。请注意,这是未经测试的,因为我只是复制并粘贴了这些示例。

在您的原始文件中进行以下更改 -

$sql = "SELECT CourseName from Course ";
$result = mysql_query ($sql, $connection);

  echo "<tr><th>Course Name </th>";
  echo "<td><select id='CourseName' name='v1' onchange='students()' >"; // Added on Change
  echo "<option value='' selected='selected'>Select</option> ";
  while( $row = mysql_fetch_array($result))
  {
  echo "<option value='$row[CourseID]'>$row[CourseName]</option> ";  // make the value = to CourseID
  }
 echo "</select>";
 echo "</td>";
 echo "</tr>" ;

 // removed sql query to get students, 

 echo "<tr><th>Student Name </th>";
 echo "<td><select id='StudentName' name='v2' >";  //builds a blank student select
 echo "</select>";
 echo "</td>";
 echo "</tr>" ;

 echo "</table>" ;

在文件的头部添加以下 javascript/jQuery/Ajax

<head>
...
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script> //can be changed to other version ie. 1.7.../1.8.. etc
<script type="text/javascript">
function students(){   
$("#StudentName option").remove();  // empties out the StudentName select so we can rebuild
var course = $('#CourseName').val();  //this gets the value of the selected Course
if(course != ""){ // we will only get students if a Course is selected
  jQuery.ajax({
  type: "POST",
  url: "students.php",
  data: 'course='+course,
  cache: false,
  success: function(response){
  var student_array = JSON.parse(response);
  $.each(student_array, function() {
  $("<option />").attr("value", student_array).text(student_array).appendTo("#StudentName");  // adds new StudentName select options
  });
  }
});
}
</script>
...
</head>

并创建一个单独的文件students.php- (或url在 Ajax 中重命名以匹配)

<?php
include('../connect.php');
$course = mysql_real_escape_string($_POST['course']); // gets course that was sent via Ajax
$sql  = "SELECT StudentName from Student";
$sql .= " WHERE CourseID = '$course'";  // get only students with selected CourseID
$result = mysql_query ($sql, $connection);
 while( $row = mysql_fetch_array($result))
 {
    $student_array[] = $row[StudentName];
 }
 $student_array = json_encode($student_array);  // encodes it to send back
 print_r($student_array);   // prints the array to use
 ?>

请注意,您不应该使用mysql_*函数编写新代码。相反,应该使用MySQLior扩展名。PDO_MySQL请参阅MySQL:选择 API 指南

于 2012-11-12T03:59:19.937 回答