2

下面我有一个 php 脚本,它显示一个“课程”下拉菜单和一个“模块”下拉菜单。假设发生的是用户首先从“课程”下拉菜单中选择一门课程,然后属于所选课程的模块列表将出现在“模块”下拉菜单中。下面是这个的代码:

create_session.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">'.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>'; 

?>


<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
    <table>
        <tr>
            <th>Course: <?php echo $courseHTML; ?>
                <input id="courseSubmit" type="submit" value="Submit" name="submit" />
            </th>
        </tr>
    </table>
</form>

<?php

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

    $submittedCourseId = $_POST['courses'];

    $query = "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.CourseName, m.ModuleId";

    $qrystmt=$mysqli->prepare($query);

    // You only need to call bind_param once
    $qrystmt->bind_param("s",$submittedCourseId);
    // get result and assign variables (prefix with db)

    $qrystmt->execute(); 

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

    $qrystmt->store_result();

    $num = $qrystmt->num_rows();

    if($num ==0){
        echo "<p style='color: red'>Please Select a Course</p>";
    } else { 

        $dataArray = array();

        while ( $qrystmt->fetch() ) { 
            // data array
            $dataArray[$dbCourseId]['CourseName'] = $dbCourseName; 
            $dataArray[$dbCourseId]['CourseNo'] = $dbCourseNo; 
            $dataArray[$dbCourseId]['Modules'][$dbModuleId]['ModuleName'] = $dbModuleName; 
            $dataArray[$dbCourseId]['Modules'][$dbModuleId]['ModuleNo'] = $dbModuleNo; 

            // session data
            $_SESSION['idcourse'] = $dbCourseNo;
            $_SESSION['namecourse'] = $dbCourseName;
            $_SESSION['idmodule'] = $dbModuleNo;
            $_SESSION['namemodule'] = $dbModuleName;

            }

        foreach ($dataArray as $foundCourse => $courseData) {

            $output = ""; 

            $output .= "<p><strong>Course:</strong> " . $courseData['CourseNo'] .  " - "  . $courseData['CourseName'] . "</p>";

            $moduleHTML = ""; 
            $moduleHTML .= '<select name="module" id="modulesDrop">'.PHP_EOL;
            $moduleHTML .= '<option value="">Please Select</option>'.PHP_EOL;      
            foreach ($courseData['Modules'] as $moduleId => $moduleData) {        

                $moduleHTML .= "<option value='$moduleId'>" . $moduleData['ModuleNo'] . " - " . $moduleData['ModuleName'] ."</option>".PHP_EOL;        
            } 
        }
    $moduleHTML .= '</select>';

    echo $output;

?>

<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
    <table>
        <tr>
            <th>Course: <?php echo $moduleHTML; ?>
                <input id="courseSubmit" type="submit" value="Submit" name="submit" />
            </th>
        </tr>
    </table>
</form>

因此,假设我INFO101 - Information Communication Technology在“课程”下拉菜单中选择了课程,它会在下面的模块下拉菜单中显示与该课程对应的以下模块:

CHI2520 - Advanced Web Programming
CHI2220 - Systems Strategy
CHI2350 - Interactive Systems

现在这是我遇到的问题。如果我选择模块CHI2520 - Advanced Web Programming然后访问下面的页面,它会显示这个模块CHI2350 - Interactive Systems

QandATable.php:

    <?php

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

        $_SESSION['idmodule'] = $_POST['idmodule'];

    }

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

        $_SESSION['namemodule'] = $_POST['namemodule'];

    }

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

$_SESSION['idcourse'] = $_POST['idcourse'];

}

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

$_SESSION['namecourse'] = $_POST['namecourse'];

}


    $outputDetails = "";
    $outputDetails .= "
    <table id='sessionDetails' border='1'>
    <tr>
    <th>Course:</th> 
    <th>{$_SESSION['idcourse']}  {$_SESSION['namecourse']}</th>
    </tr>
<tr>
<th>Module:</th> 
<th>{$_SESSION['idmodule']}  {$_SESSION['namemodule']}</th>
</tr>
    </table>
    ";

    echo $outputDetails;

    ?>

我的问题是为什么它在另一页显示错误的模块编号和名称?两个页面都包含session_start();

下面是导航到 QandATable.php 的表单:

<form action="QandATable.php" method="post" id="sessionForm">
            <table><tr><th>6: Module:</th>
            <td><?php echo $moduleHTML; ?></td>
            </tr>
            </table>
            <p><input class="questionBtn" type="submit" value="Prepare Questions" name="prequestion" /></p>   


        </form>
4

1 回答 1

0

将选择设置为 autocomplete="off",它应该可以解决此问题。浏览器的自动完成功能试图让用户保持原样,而不是告诉它不这样做。它把我逼疯了。

<select autocomplete="off" name="courses" id="coursesDrop">
于 2012-11-26T19:10:19.053 回答