2012-11-11 193 views
0

我有一个下拉所谓的“课程”的选项(math101,eng102 ..等) 列表,我想提出所谓的(学生姓名)另一个下拉列表,但是这个应该隐藏一个,所以当用户从课程列表中选择一个值时,学生姓名的列表现在将显示只显示该课程的学生姓名,以便用户可以选择一个。当然,所有数据都将从数据库PHP - 显示/隐藏下拉列表中,由于选择

到目前为止我的代码是

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

+1

你通常会在客户端使用Javascript/jQuery处理这种东西。 – Throttlehead

+0

是的,雅各布是对的,你需要JS和AJAX。 –

回答

0

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

在原始文件进行以下changes-

$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的/阿贾克斯

<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以匹配)

<?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_*函数编写新代码。应该使用MySQLiPDO_MySQL扩展名。见MySQL: choosing an API guide

+0

为什么要删除sql查询来获取学生?我想让学生的姓名作为选择选项! – user1806136

+0

你想获得所选课程的学生名单。直到用户选择课程才会知道该课程,因此学生名单不是最初生成的。相反,它是在用户选择课程后在单独的查询中创建的。 – mattc

+0

我删除了初始的学生sql查询,因为在选择课程之后才应该填充它。我会发布另一种方式来做到这一点在PHP只有1个JavaScript代码。 – Sean

0

这里是另一种方式来做到这一点,如果你不喜欢使用我提供的阿贾克斯/ jQuery的答案。选择课程后,它将根据CourseID基于StudentName的选择重新加载页面。这是通过将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>" ; 

?> 
相关问题