代码更改导致下拉菜单中的选项不显示

时间:2012-11-24 21:51:38

标签: php mysqli

好的,如果你看一下editsessionteacher.php脚本,它包含下面的代码,它在下拉菜单中显示以下值:

$outputmodule = ""; 

        $moduleInfo = explode("_", $_POST['modules']);
        $moduleId = $moduleInfo[0];
        $moduleName = $moduleInfo[1];
        $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:

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


<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" onsubmit="return validation();">
<table>
<tr>
<th>Course: <?php echo $courseHTML; ?></th>
<th>Module: <?php echo $moduleHTML; ?></th>
</tr>
</table>
<p><input id="moduleSubmit" type="submit" value="Submit Course and Module" name="moduleSubmit" /></p>
</form>

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

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

我的问题是,当我更改代码以添加模块编号时,为什么它不会在下拉菜单中显示任何内容。

1 个答案:

答案 0 :(得分:0)

我不明白为什么您的下拉菜单没有显示,但我确实看到您修改后的module.php sprintf()存在问题,以及它在editsessionteacher.php中的显示方式。< / p>

原始module.php sprintf()传递了2个使用'_'连接的值

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

和您的editsessionteacher.php explode()这两个值

$moduleInfo = explode("_", $_POST['modules']);
$moduleId = $moduleInfo[0];
$moduleName = $moduleInfo[1];

但您修改后的module.php sprintf()只传递了1个值 -

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

但您的editsessionteacher.php正在尝试explode() 3个值

$moduleInfo = explode("_", $_POST['modules']);
$moduleId = $moduleInfo[0];
$moduleNo = $moduleInfo[1];
$moduleName = $moduleInfo[2];

基于editsessionteacher.php explode(),请尝试将module.php sprintf()更新为 -

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