在下面的代码中,我有一个查询,它从db中检索所有模块,然后在下拉菜单中将它们列为选项。现在假设发生的是用户从下拉菜单中选择一个模块并提交。提交后,它将显示在用户选择的模块下方。
问题在于,无论用户从下拉菜单中选择了哪个模块,它始终指出用户已选择Advanced Web programming
模块。我的问题是为什么总是说明这一点而不是显示选择的正确模块?
以下是VIEW SOURCE代码:
<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>
以下是书面代码:
$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 } ?>
答案 0 :(得分:0)
我的PHP有点生疏,但我相信这是因为模块Advanced Web Programming
是列表中的最后一个。每次通过while循环从DB中获取行时,您的变量$dbModuleNo
和$dbModuleName
都会更新,因此当您想要显示所选模块时,这些变量包含最近获取的数据,不是你想要的选定数据。
尝试这样的事情(我在现有代码中添加了几行):
<?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;?>