2012-11-24 55 views
1

我不認爲我正確寫下面的代碼,但我想能夠從另一頁上的模塊下拉菜單中檢索值。下面是其含有一個空白下拉菜單,並且輸出模塊的選擇的號碼和名稱的代碼:下拉菜單不顯示任何選項

$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; 

回答

0

看來,你永遠不會調用JavaScript函數getModules()這將使獲得新的選擇請求。試試你的<script>標籤中添加此:由while迴路中產生

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

}) 
+0

那麼,什麼是改變我在這兩個腳本代碼,以便能夠最好的方式讓下拉菜單再次工作? – user1819709

+0

行..我誤解了上面的代碼是在2個文件中。其實'select'的關閉是正確的。此外,jQuery Ajax看起來是正確的。看起來你永遠不會調用'getModules()'函數。給我幾分鐘的時間來更新我的答案 – charlietfl

+0

答案已更新並完全更改 – charlietfl

0

字符串應該是<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; 
+0

好的,但我怎麼能包括它在我的代碼,因爲我想它顯示下拉菜單上直接在頁面顯示「請選擇」選項,然後當用戶選擇課程時,我希望它包含其他選項在下拉菜單中 – user1819709