下面我有一个php / mysqli代码块,它会创建一个下拉菜单:
$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">%1$s - %2$s</option>'.PHP_EOL, $dbModuleNo, $dbModuleName, $dbModuleId);
}
$moduleHTML .= '</select>';
这个的HTML是:
<select name="modules" id="modulesDrop">
<option value="">Please Select</option>
</select>
现在我尝试在用户选择了Please Select
选项时显示消息。但问题是没有显示消息。我的问题是,如果用户选择Please Select
,我该如何显示消息,说明需要选择模块?
以下是主要代码:
<?php
...
$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">%1$s - %2$s</option>'.PHP_EOL, $dbModuleNo, $dbModuleName, $dbModuleId);
}
$moduleHTML .= '</select>';
$pHTML = "";
?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<table>
<tr>
<th>Module: <?php echo $moduleHTML; ?></th>
</tr>
</table>
<p><input id="moduleSubmit" type="submit" value="Submit Module" name="moduleSubmit" /></p>
</form>
<?php
if (isset($_POST['moduleSubmit'])){
if (isset($_POST['modules']) && $_POST['modules'] == '' ){
$pHTML = "<span style='color: red'>Please Select a Module</span>";
}else{
$assessmentform = "<div id='lt-container'>
<form action='".htmlentities($_SERVER['PHP_SELF'])."' method='post' id='assessmentForm'>
<p id='warnings'>{$pHTML}</p>
<p><strong>View Assessment Details:</p>
name='sessionSubmit' /></p>
</form>
</div>";
echo $assessmentform;
}
}
?>
答案 0 :(得分:1)
您已为请选择选项 value =“”。所以你可以直接检查元素。而且,你已经做到了。请检查一下
行if (isset($_POST['modules']) && $_POST['modules'] == '' ){
$pHTML = "<span style='color: red'>Please Select a Module</span>";
}else{
此行将检查用户是否选择了任何值。
答案 1 :(得分:0)
在下面的代码中使用带有标记粗体的echo语句
if (isset($_POST['moduleSubmit'])){
if (isset($_POST['modules']) && $_POST['modules'] == '' ){
**echo** $pHTML = "<span style='color: red'>Please Select a Module</span>";
}else{