下拉菜单未显示任何选项

时间:2012-11-24 01:15:16

标签: php jquery mysqli

我不认为我正在编写下面的代码,但我希望能够从另一页的模块下拉菜单中检索该值。下面是包含空白下拉菜单的代码,并输出所选模块的编号和名称:

$sql = "SELECT CourseId, CourseNo, CourseName FROM Course"; 

 $sqlstmt=$mysqli->prepare($sql);

 $sqlstmt->execute(); 

 $sqlstmt->bind_result($dbCourseId, $dbCourseNo, $dbCourseName);

 $courses = array(); // easier if you don't use generic names for data 

 $courseHTML = "";  
 $courseHTML .= '<select name="courses" id="coursesDrop" onchange="getModules();">'.PHP_EOL; 
 $courseHTML .= '<option value="">Please Select</option>'.PHP_EOL;  

 while($sqlstmt->fetch()) 
 { 
     $courseno = $dbCourseNo;
     $course = $dbCourseId;
     $coursename = $dbCourseName; 
     $courseHTML .= "<option value='".$course."'>" . $courseno . " - " . $coursename . "</option>".PHP_EOL;  
  } 

  $courseHTML .= '</select>'; 

    $moduleHTML = "";  
        $moduleHTML .= '<select name="modules" id="modulesDrop">'.PHP_EOL; 
        $moduleHTML .= '<option value="">Please Select</option>'.PHP_EOL;  
        $moduleHTML .= '</select>'; 


        ?>

        <script type="text/javascript">

        function getModules() { 
        var course = jQuery("#coursesDrop").val(); 
        jQuery('#modulesDrop').empty(); 
        jQuery('#modulesDrop').html('<option value="">Please Select</option>'); 
        jQuery.ajax({ 
        type: "post", 
        url:  "module.php", 
        data: { course:course }, 
        success: function(response){ 
        jQuery('#modulesDrop').append(response); 
        } 
        }); 


        }


        </script> 

    <?php

    if (isset($_POST['moduleSubmit'])) {    

    $outputmodule = ""; 

    $moduleInfo = explode("_", $_POST['modules']);
    $moduleId = $moduleInfo[0];
    $moduleNo = $moduleInfo[1];
    $moduleName = $moduleInfo[2];
    $outputmodule = sprintf("<p><strong>Module:</strong> %s - %s</p>", $moduleNo, $moduleName);

    ....

    ?>

下面是module.php页面,它根据所选课程在下拉菜单中显示模块编号和名称:

<?php

$course = isset($_POST['course']) ? $_POST['course'] : ''; 

    $sql = "
    SELECT cm.CourseId, cm.ModuleId, c.CourseNo, m.ModuleNo,
    c.CourseName,
    m.ModuleName
    FROM Course c
    INNER JOIN Course_Module cm ON c.CourseId = cm.CourseId
    JOIN Module m ON cm.ModuleId = m.ModuleId
    WHERE
    (c.CourseId = ?)
    ORDER BY c.CourseId, m.ModuleId
    "; 

     $sqlstmt=$mysqli->prepare($sql);

     $sqlstmt->bind_param("s",$course);

     $sqlstmt->execute(); 

     $sqlstmt->bind_result($dbCourseId,$dbModuleId,$dbCourseNo,$dbModuleNo,$dbCourseName,$dbModuleName);


    $moduleHTML  = "";  

     while($sqlstmt->fetch()) { 
         $moduleHTML .= sprintf('<option value="%1$s">%2$s - %3$s</option>'.PHP_EOL, $dbModuleId, $dbModuleNo, $dbModuleName);
    } 


    echo $moduleHTML; 

     $sqlstmt->execute(); 
    ?>

目前,除了“请选择”选项

外,下拉菜单中没有显示任何内容

更新:

好的,如果你看一下editsessionteacher.php脚本,它包含下面的代码:

$outputmodule = ""; 

        $moduleInfo = explode("_", $_POST['modules']);
        $moduleId = $moduleInfo[0];
        $moduleName = $moduleInfo[2];
        $outputmodule = sprintf("<p><strong>Module:</strong> %s - %s</p>", $moduleId, $moduleName);

module.php是这样的:

$course = isset($_POST['course']) ? $_POST['course'] : ''; 

$sql = "
SELECT cm.CourseId, cm.ModuleId,
c.CourseName,
m.ModuleName
FROM Course c
INNER JOIN Course_Module cm ON c.CourseId = cm.CourseId
JOIN Module m ON cm.ModuleId = m.ModuleId
WHERE
(c.CourseId = ?)
ORDER BY c.CourseId, m.ModuleId
"; 

 $sqlstmt=$mysqli->prepare($sql);

 $sqlstmt->bind_param("s",$course);

 $sqlstmt->execute(); 

 $sqlstmt->bind_result($dbCourseId,$dbModuleId,$dbCourseName,$dbModuleName);


$moduleHTML  = "";  

     while($sqlstmt->fetch()) { 
         $moduleHTML .= sprintf('<option value="%1$s_%2$s">%1$s - %2$s</option>'.PHP_EOL, $dbModuleId, $dbModuleName);
    } 


echo $moduleHTML; 

 $sqlstmt->execute();

但是我想将moduleNo添加到下拉菜单中,所以当我尝试将代码更改为以下内容时,它不起作用:

editsessionteacher.php:

$outputmodule = ""; 

$moduleInfo = explode("_", $_POST['modules']);
$moduleId = $moduleInfo[0];
$moduleNo = $moduleInfo[1];
$moduleName = $moduleInfo[2];
$outputmodule = sprintf("<p><strong>Module:</strong> %s - %s</p>", $moduleNo, $moduleName);

module.php:

    $course = isset($_POST['course']) ? $_POST['course'] : ''; 

    $sql = "
    SELECT cm.CourseId, cm.ModuleId, c.CourseNo, m.ModuleNo,
    c.CourseName,
    m.ModuleName
    FROM Course c
    INNER JOIN Course_Module cm ON c.CourseId = cm.CourseId
    JOIN Module m ON cm.ModuleId = m.ModuleId
    WHERE
    (c.CourseId = ?)
    ORDER BY c.CourseId, m.ModuleId
    "; 

     $sqlstmt=$mysqli->prepare($sql);

     $sqlstmt->bind_param("s",$course);

     $sqlstmt->execute(); 

     $sqlstmt->bind_result($dbCourseId,$dbModuleId,$dbCourseNo,$dbModuleNo,$dbCourseName,$dbModuleName);


    $moduleHTML  = "";  

$moduleHTML .= '<select name="modules" id="modulesDrop">'.PHP_EOL; 
$moduleHTML .= '<option value="">Please Select</option>'.PHP_EOL;  

 while($sqlstmt->fetch()) { 
     $moduleHTML .= sprintf('<option value="%1$s">%2$s - %3$s</option>'.PHP_EOL, $dbModuleId, $dbModuleNo, $dbModuleName);
} 

$moduleHTML .= '</select>'; 

echo $moduleHTML; 

2 个答案:

答案 0 :(得分:0)

看起来您从未调用过javascript函数getModules(),这会使请求获得新选项。请尝试在<script>标记中添加:

jQuery(function(){
  jQuery("#coursesDrop").change(getModules); 

})

答案 1 :(得分:0)

while循环生成的字符串应位于<select>...</select>标记

$moduleHTML .= '<select name="modules" id="modulesDrop">'.PHP_EOL; 
$moduleHTML .= '<option value="">Please Select</option>'.PHP_EOL;  

 while($sqlstmt->fetch()) { 
     $moduleHTML .= sprintf('<option value="%1$s">%2$s - %3$s</option>'.PHP_EOL, $dbModuleId, $dbModuleNo, $dbModuleName);
} 

$moduleHTML .= '</select>'; 

echo $moduleHTML;