Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-根据选择显示/隐藏下拉列表_Php_Drop Down Menu_Hide_Selection - Fatal编程技术网

PHP-根据选择显示/隐藏下拉列表

PHP-根据选择显示/隐藏下拉列表,php,drop-down-menu,hide,selection,Php,Drop Down Menu,Hide,Selection,我有一个名为“课程”的下拉列表,带有选项(math101、eng102等) 我想放另一个名为(学生姓名)的下拉列表,但这个列表应该是隐藏的,所以当用户从课程列表中选择一个值时,学生姓名的列表现在会显示只上过这门课程的学生姓名,以便用户可以选择一个。。当然,所有数据都将从数据库中获取 到目前为止,我的代码是 <?php include('../connect.php'); $id=$_SESSION['login_user']; $sql = "SELECT

我有一个名为“课程”的下拉列表,带有选项(math101、eng102等) 我想放另一个名为(学生姓名)的下拉列表,但这个列表应该是隐藏的,所以当用户从课程列表中选择一个值时,学生姓名的列表现在会显示只上过这门课程的学生姓名,以便用户可以选择一个。。当然,所有数据都将从数据库中获取

到目前为止,我的代码是

<?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>" ;

    ?>

我这样做的方法是使用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>" ;

请注意,您不应该使用
mysql.*
函数编写新代码。相反,应该使用
MySQLi
PDO_-MySQL
扩展。如果您不想使用我提供的Ajax/jQuery答案,请参阅这里的另一种方法。选择课程后,它将使用
StudentName
根据
CourseID
选择重新加载页面。这是通过添加1个javascript代码
onchange='This.form.submit();'
CourseID
select,然后在
StudentName
select周围添加
isset($\u POST['v1'])

<?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>" ;

?>

您通常会使用Javascript/jQuery在客户端处理此类内容。是的,Jacob是对的,您将需要JS,可能还需要AJAX。为什么要删除sql query来获取学生?我想让学生的名字作为选择选项!您希望获得所选课程的学生列表。在用户选择课程之前,课程是未知的,因此最初不会生成学生列表。相反,它是在用户选择课程后在单独的查询中创建的。我删除了初始的student sql查询,因为在选择课程之前不应该填充它。我将发布另一种在php中实现这一点的方法,只需1个javascript代码。
<?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
 ?>
<?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>" ;

?>