0

In code below I have a query which retrives all of the modules from db and then list them as options in a drop down menu. Now what is suppose to happen is that user selects a module from the drop down menu and submit. After submit it will display underneath the module the user has selectd.

The problem is that no matter which module the user has chosen from the drop down menu, it always states that the user has selected the Advanced Web programming module. My question is why is it always stating this and not instead displaying the correct module chosen?

Below is the VIEW SOURCE code:

<form action="" method="post">Module:
  <select name="module" id="modulesDrop">
    <option value="">Please Select</option>
    <option selected='selected' value='CHI2513_Systems Strategy_1'>CHI2513 - Systems Strategy</option>
    <option value='CHI2565_Ecommerce, Business and Technology_3'>CHI2565 - Ecommerce, Business and Technology</option>
    <option value='CHT2220_Interactive Systems_4'>CHT2220 - Interactive Systems</option>
    <option value='CHT2520_Advanced Web Programming_5'>CHT2520 - Advanced Web Programming</option>
  </select>
  <input id="moduleSubmit" type="submit" value="Submit Module" name="moduleSubmit" />
</form>
<form action="" method="post">
  <p> <strong>Selected Module:</strong>CHT2520 - Advanced Web Programming
    <input type='text' value='CHI2513_Systems Strategy_1' />
  </p>
  Asessments: <span class='red'>Sorry, You have No Assessments under this Module</span>

Below is the written code:

$moduleactive = 1;
            $sql = "SELECT ModuleId, ModuleNo, ModuleName FROM Module WHERE ModuleActive = ? ORDER BY ModuleNo"; 

            $sqlstmt=$mysqli->prepare($sql);
            $sqlstmt->bind_param("i", $moduleactive);
            $sqlstmt->execute(); 
            $sqlstmt->bind_result($dbModuleId,$dbModuleNo,$dbModuleName);
            $sqlstmt->store_result();
            $sqlnum = $sqlstmt->num_rows();
            ?>
            Module: 
            <select name="module" id="modulesDrop">
                <option value="">Please Select</option>
                <?php
                while($sqlstmt->fetch()) { 
                    $ov = $dbModuleNo . "_" . $dbModuleName . "_" . $dbModuleId; 
                    if($ov == $_POST["module"]) 
                        echo "<option selected='selected' value='$ov'>$dbModuleNo - $dbModuleName</option>" . PHP_EOL; 
                    else 
                        echo "<option value='$ov'>$dbModuleNo - $dbModuleName</option>" . PHP_EOL;
                 } 
                ?>
            </select>
            <input id="moduleSubmit" type="submit" value="Submit Module" name="moduleSubmit" />
        </form>
        <?php
        if(isset($_POST['moduleSubmit'])) 
{


if($_POST["module"] != "") { ?>
        <form action="" method="post"> 
            <p>
            <strong>Selected Module:</strong><?php echo $dbModuleNo .' - '. $dbModuleName;?> <input type='text' value='<?php echo $_POST["module"];?>'>
            </p>
            <?php } ?>
4

1 回答 1

0

我对我的 PHP 有点生疏,但我相信这是因为该模块Advanced Web Programming在列表中的最后一个。您的变量$dbModuleNo$dbModuleName每次运行 while 循环从数据库中获取行时都会更新,因此当您想要显示所选模块时,这些变量包含最近获取的数据,而不是您想要的所选数据。

尝试这样的事情(我在您现有的代码中添加了几行):

<?php
$selectedText = "None";
while($sqlstmt->fetch()) { 
    $ov = $dbModuleNo . "_" . $dbModuleName . "_" . $dbModuleId; 
    if($ov == $_POST["module"]) 
        echo "<option selected='selected' value='$ov'>$dbModuleNo - $dbModuleName</option>" . PHP_EOL;
        $selectedText = $dbModuleNo .' - '. $dbModuleName;
    else 
        echo "<option value='$ov'>$dbModuleNo - $dbModuleName</option>" . PHP_EOL;
} 
?>

然后后来...

<strong>Selected Module:</strong><?php echo $selectedText;?>
于 2013-01-17T14:56:12.533 回答