我正在研究这个4链式下拉选项,一切正常,这就是它的工作原理。
学期:你选择一个学期
主题:选择一个学期后,选择一个主题
部分:选择主题后,您可以选择哪个部分
教授:选择一个部分后,根据所选部分选择哪个教授。
问题在于,当涉及到教授时,它会从数据库中取代所有教授,而不是指派给该课程和部分的教授。例如,我这样做:
学期:2015年秋季
主题:csis 330
部分:1
教授:应该展示杰森,但它显示所有教授1号部分的教授(问题是它需要检查教授所选科目和所选科目的教授,不仅仅是因为它带来所有教授教授科目编号1)
我希望你明白了。
以下是我的代码:
func.php
<html>
<?php
//**************************************
// Page load dropdown results //
//**************************************
function getTierOne()
{
$result = mysql_query("SELECT DISTINCT semester FROM three_drops")
or die(mysql_error());
while($tier = mysql_fetch_array( $result ))
{
echo '<option value="'.$tier['semester'].'">'.$tier['semester'].'</option>';
}
}
//**************************************
// First selection results //
//**************************************
if($_GET['func'] == "drop_1" && isset($_GET['func'])) {
drop_1($_GET['drop_var']);
}
function drop_1($drop_var)
{
include_once('db.php');
$result = mysql_query("SELECT DISTINCT subject FROM three_drops WHERE semester='$drop_var'")
or die(mysql_error());
?>
<br /><br /> <label for="subject" accesskey="s">Subject</label>
<?php
echo '<select name="drop_2" id="drop_2">
<option value=" " disabled="disabled" selected="selected">Choose one</option>';
while($drop_2 = mysql_fetch_array( $result ))
{
echo '<option value="'.$drop_2['subject'].'">'.$drop_2['subject'].'</option>';
}
echo '</select>';
echo "<script type=\"text/javascript\">
$('#wait_2').hide();
$('#drop_2').change(function(){
$('#wait_2').show();
$('#result_2').hide();
$.get(\"func.php\", {
func: \"drop_2\",
drop_var: $('#drop_2').val()
}, function(response){
$('#result_2').fadeOut();
setTimeout(\"finishAjax_section('result_2', '\"+escape(response)+\"')\", 400);
});
return false;
});
</script>";
}
//**************************************
// Second selection results //
//**************************************
if($_GET['func'] == "drop_2" && isset($_GET['func'])) {
drop_2($_GET['drop_var']);
}
function drop_2($drop_var)
{
include_once('db.php');
$result = mysql_query("SELECT DISTINCT section FROM three_drops WHERE subject='$drop_var'")
or die(mysql_error());
?>
<br /><br /> <label for="section" accesskey="s">Section</label>
<?php
echo '<select name="drop_3" id="drop_3">
<option value=" " disabled="disabled" selected="selected">Choose one</option>';
while($drop_3 = mysql_fetch_array( $result ))
{
echo '<option value="'.$drop_3['section'].'">'.$drop_3['section'].'</option>';
}
echo '</select>';
echo "<script type=\"text/javascript\">
$('#wait_3').hide();
$('#drop_3').change(function(){
$('#wait_3').show();
$('#result_3').hide();
$.get(\"func.php\", {
func: \"drop_3\",
drop_var: $('#drop_3').val()
}, function(response){
$('#result_3').fadeOut();
setTimeout(\"finishAjax_professor('result_3', '\"+escape(response)+\"')\", 400);
});
return false;
});
</script>";
}
//**************************************
// Third selection results //
//**************************************
if($_GET['func'] == "drop_3" && isset($_GET['func'])) {
drop_3($_GET['drop_var']);
}
function drop_3($drop_var)
{
include_once('db.php');
$result = mysql_query("SELECT DISTINCT professor FROM three_drops WHERE section='$drop_var'")
or die(mysql_error());
?>
<br /><br /> <label for="professor" accesskey="s">Professor</label>
<?php
echo '<select name="drop_4" id="drop_4">
<option value=" " disabled="disabled" selected="selected">Choose one</option>';
while($drop_4 = mysql_fetch_array( $result ))
{
echo '<option value="'.$drop_4['professor'].'">'.$drop_4['professor'].'</option>';
}
echo '</select> ';
}
?>
</html>
的index.php
<?php
include('db.php');
include('func.php');
?>
<div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#wait_1').hide();
$('#drop_1').change(function(){
$('#wait_1').show();
$('#result_1').hide();
$.get("func.php", {
func: "drop_1",
drop_var: $('#drop_1').val()
}, function(response){
$('#result_1').fadeOut();
setTimeout("finishAjax('result_1', '"+escape(response)+"')", 400);
});
return false;
});
});
function finishAjax(id, response) {
$('#wait_1').hide();
$('#'+id).html(unescape(response));
$('#'+id).fadeIn();
}
function finishAjax_section(id, response) {
$('#wait_2').hide();
$('#'+id).html(unescape(response));
$('#'+id).fadeIn();
}
function finishAjax_professor(id, response) {
$('#wait_3').hide();
$('#'+id).html(unescape(response));
$('#'+id).fadeIn();
}
</script>
<label for="semester" accesskey="s">Semester</label>
<select name="drop_1" id="drop_1">
<option value="" selected="selected" disabled="disabled">Select a Category</option>
<?php getTierOne(); ?>
</select>
<span id="wait_1" style="display: none;">
<img alt="Please Wait" src="ajax-loader.gif"/>
</span>
<span id="result_1" style="display: none;"></span>
<span id="wait_2" style="display: none;">
<img alt="Please Wait" src="ajax-loader.gif"/>
</span>
<span id="result_2" style="display: none;"></span>
<span id="wait_3" style="display: none;">
<img alt="Please Wait" src="ajax-loader.gif"/>
</span>
<span id="result_3" style="display: none;"></span>
</div>
数据库是这样的:
semester | subject | section | professor
Fall-2015 | ELEG-330 | 1 | Jason
Fall-2015 | ELEG-330 | 2 | Michael
Fall-2015 | ELEG-210 | 1 | kyle
例如上面的例子,如果学期是2015年秋季,主题是eleg-330和第1部分,教授会显示jason和kyle都不正确。
提前致谢。
答案 0 :(得分:2)
首先,您需要更改$.get()
中的function drop_2($drop_var)
,以便传递其他数据
function drop_2($drop_var)
{
include_once('db.php');
...[your other code]...
echo '</select>';
echo "<script type=\"text/javascript\">
$('#wait_3').hide();
$('#drop_3').change(function(){
$('#wait_3').show();
$('#result_3').hide();
$.get(\"func.php\", {
func: \"drop_3\",
drop_var1: $('#drop_1').val(), //semester
drop_var2: $('#drop_2').val(), //subject
drop_var3: $('#drop_3').val() //section
}, function(response){
$('#result_3').fadeOut();
setTimeout(\"finishAjax_professor('result_3', '\"+escape(response)+\"')\", 400);
});
return false;
});
</script>";
}
然后在function drop_3()
中,您需要添加其他参数
//**************************************
// Third selection results //
//**************************************
if($_GET['func'] == "drop_3" && isset($_GET['func'])) {
drop_3($_GET['drop_var1'],$_GET['drop_var2'],$_GET['drop_var3']);
}
function drop_3($drop_var1,$drop_var2,$drop_var3)
{
include_once('db.php');
$result = mysql_query("SELECT DISTINCT professor FROM three_drops WHERE semester='$drop_var1' AND subject='$drop_var2' AND section='$drop_var3'")
or die(mysql_error());
?>
...[rest of your code]...
}
答案 1 :(得分:2)
您没有在每个后续步骤中传递先前的信息。将主题传递给第三个函数,将其添加到get中,如下所示:
$('#drop_3').change(function(){
$('#wait_3').show();
$('#result_3').hide();
$.get(\"func.php\", {
func: \"drop_3\",
drop_var: $('#drop_3').val(),
prev_var: '".$drop_var."' // added $prev_var here
}, function(response){
$('#result_3').fadeOut();
setTimeout(\"finishAjax_professor('result_3', '\"+escape(response)+\"')\", 400);
});
return false;
});
然后将其传递给第三个函数中的第二个参数,并在查询中使用它:
if($_GET['func'] == "drop_3" && isset($_GET['func'])) {
drop_3($_GET['drop_var'], $_GET['prev_var');
}
function drop_3($drop_var, $prev_var) // $prev_var here
{
include_once('db.php');
$result = mysql_query("SELECT DISTINCT professor FROM three_drops WHERE section='$drop_var' AND subject='$prev_var'") // $prev_var here
or die(mysql_error());
//etc.
}