未显示消息是选择的选项是“请选择”

时间:2013-01-13 18:58:07

标签: php html

我在这里有一个下拉菜单:

        $pHTML = "";

             $moduledrop = isset($_POST['modules']) ? $_POST['modules'] : '';

                    $moduleHTML .= '<select name="modules" id="modulesDrop">'.PHP_EOL;
                 $moduleHTML .= '<option value="">Please Select</option>'.PHP_EOL;  
                ....
                $moduleHTML .= '</select>'; 

....

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

我的问题很简单,如果用户选择Please Select下拉菜单并提交,则应显示Please Select a Module消息。但它没有显示此消息。为了做到这一点,我需要在下面的代码中进行更改:

     if (isset($_POST['moduleSubmit']) || isset($_POST['sessionSubmit'])){  

         if($_POST['modules'] == ''){
            $pHTML = "<span style='color: red'>Please Select a Module</span>";
            }else if($sqlnum == 0){
            $pHTML = "<span style='color: red'>Sorry, You have No Assessments under this Module</span>";
            } else{
            $pHTML = "<span style='color: green'>Assessment Successfully Found</span>";

                $assessmentform = "<div id='lt-container'>
                <form action='".htmlentities($_SERVER['PHP_SELF'])."' method='post' id='assessmentForm'>
                <p id='warnings'>{$pHTML}</p>
  <p><input id="sessionSubmit" type="submit" value="Submit Assessment" name="sessionSubmit" /></p>
                </form>
                </div>";

                echo $assessmentform;

                }

            }

1 个答案:

答案 0 :(得分:0)

我不确定你的整个脚本究竟是什么样的,并且很难说出你的示例中的代码所在,但我猜$moduledrop = isset($_POST['modules']) ? $_POST['modules'] : '';导致了这个问题。

您需要将所有代码一起发布。很难看出到底发生了什么。我看到的只有两种形式和两个地方检查是否提交了代码。我只是把它放在一起,让你知道你需要做什么。以下代码有效。如果您仍然需要帮助,请发布整页,以便我知道我在读什么。

尝试:

<?php

$pHTML = "";
$moduledrop = isset($_POST['modules']) ? $_POST['modules'] : '';
$moduleHTML .= '<select name="modules" id="modulesDrop">' . PHP_EOL;
$moduleHTML .= '<option value="">Please Select</option>' . PHP_EOL;
$moduleHTML .= '</select>';

if (!empty($_POST)) {

    if ($_POST['modules'] == '') {
        $pHTML = "<span style='color: red'>Please Select a Module</span>";
    } else if ($sqlnum == 0) {
        $pHTML = "<span style='color: red'>Sorry, You have No Assessments under this Module</span>";
    } else {
        $pHTML = "<span style='color: green'>Assessment Successfully Found</span>";
    }
    echo $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>