当显示错误信息时,如何停止显示“请选择”选项的下拉菜单

时间:2012-12-08 00:13:52

标签: php mysqli

首先我要说的是,我试图尽量减少以下代码:

        <?php
        // required variables (make them explciit no need for foreach loop)

        $getyear       = (isset($_POST['year'])) ? $_POST['year'] : '';
        $getpass       = (isset($_POST['studentpass'])) ? $_POST['studentpass'] : '';
        $getretypepass = (isset($_POST['retypepass'])) ? $_POST['retypepass'] : '';
        $errormsg      = (isset($errormsg)) ? $errormsg : '';

        $validSubmission = isset($_POST['registerbtn']) && $_POST['year'] && $_POST['studentpass'] && $_POST['retypepass'];

        $min_year = 1;
        $max_year = 10;
        $years    = range($min_year, $max_year); // returns array with numeric values of 1900 - 2012
        $yearHTML = '';
        $yearHTML .= '<select name="year" id="yearDrop">' . PHP_EOL;
        $yearHTML .= '<option value="">Please Select</option>' . PHP_EOL;
        foreach ($years as $year) {
            if (!$validSubmission && isset($_POST['year']) && $year == $_POST['year']) {
                $yearHTML .= "<option value='" . $year . "' selected='selected'>$year</option>" . PHP_EOL;
            } else {
                $yearHTML .= "<option value='" . $year . "'>$year</option>" . PHP_EOL;
            }
        }

        $yearHTML .= '</select>';


        if ((isset($_POST['registerbtn']))) {
            if (in_array($_POST['year'], $years) === true) {
                $getyear = (int) $_POST['year'];
            }
            $getpass       = $_POST['studentpass'];
            $getretypepass = $_POST['retypepass'];


            if ($getyear) {
                if ($getpass) {
                    if (strlen($getpass) <= 5) {
                        $errormsg = "The Password must be a minimum of 6 characters or more";
                    } else {
                        if ($getretypepass) {
                            if ($getpass === $getretypepass) {
                                //perform 2 queries, one query contains $aliasnumrows and other contains $numrows

                                if ($aliasnumrows == 0) {
                                    if ($numrows == 0) {
                                        //perform query which contains $numrows


                                        if ($numrows == 1) {
                                            $errormsg = "<span style='color: green'>Student has been Registered</span>";

                                            $getyear = "";


                                        } else {
                                            $errormsg = "An error has occured, Student has not been Registered";

                                        }

                                    }

                                    else {
                                        $errormsg = "There is already a Student with that Username";
                                    }
                                }

                                else {
                                    $errormsg = "There is already a Student with that Alias";
                                }
                            }

                            else {
                                $errormsg = "Your Passwords did not match";
                            }
                        }

                        else {
                            $errormsg = "You must retype your Password to Register";
                        }
                    }
                } else {
                    $errormsg = "You must enter in a Password to Register";
                }

            }

        else{
    $errormsg = "You must enter in Student's current Academic Year to Register";
    }

    }

$form = "
<form action='" . htmlentities($_SERVER["PHP_SELF"]) . "' method='post'>
  <table>
  <tr>
  <td></td>
  <td id='errormsg'>$errormsg</td>
</tr>
  <tr>
  <td>Year:</td>
  <td>{$yearHTML}</td>
  </tr>
  <tr>
  <td>Password:</td>
  <td><input type='password' name='studentpass' value='' /></td>  
  </tr>
  <tr>
  <td>Retype Password:</td>
  <td><input type='password' name='retypepass' value='' /></td>  
  </tr>
  <tr>
  <tr>
  <td></td>
  <td><input type='submit' value='Register' name='registerbtn' /></td>
  </tr>
  </table>
  </form>";

  echo $form;

好的,这是我遇到的问题。我有一个多年的下拉菜单和两个输入密码和重新输入密码的文本输入。现在,如果用户从下拉菜单中选择一年并提交表单,则会显示错误消息,指出必须输入密码,年份下拉菜单仍显示用户从年份下拉菜单中选择的选项。

我遇到的问题是,如果出现以下任何一条消息,则下拉选项会返回显示“请选择”。我的问题是,当出现以下错误消息时,如何停止下拉菜单返回“请选择”选项?

$errormsg = "There is already a Student with that Username";
$errormsg = "There is already a Student with that Alias";
$errormsg = "Your Passwords did not match";
$errormsg = "You must retype your Password to Register";

我认为需要改变的代码行是:

    $validSubmission = isset($_POST['registerbtn']) && $_POST['year'] && $_POST['studentpass'] && $_POST['retypepass'];

3 个答案:

答案 0 :(得分:1)

认为你在寻找的是:

// This is just a suggestion, but I would set these to null instead of an empty string:
    $getyear       = (isset($_POST['year'])) ? $_POST['year'] : null;
    $getpass       = (isset($_POST['studentpass'])) ? $_POST['studentpass'] : null;
    $getretypepass = (isset($_POST['retypepass'])) ? $_POST['retypepass'] : null;
    $errormsg      = (isset($errormsg)) ? $errormsg : null;
// You have already defined these, so no need to use $_POST here
// Also, I like to compare it to an actual value rather than just doing if ($variable)
// to avoid unforseen circumstances. Note, isset() returns false if $var == null.
$validSubmission = (isset($_POST['registerbtn']) && isset($getyear) && isset($getpass) && isset($getretypepass));

然后我认为你后来的逻辑倒退了:

    foreach ($years as $year) {
        if ($validSubmission && $year == $getyear) {
            $yearHTML .= "<option value='" . $year . "' selected='selected'>$year</option>" . PHP_EOL;
        } else {
            $yearHTML .= "<option value='" . $year . "'>$year</option>" . PHP_EOL;
        }
    }

答案 1 :(得分:1)

当重新填充Year下拉列表时,您可能实际上并不关心提交的任何部分是否有效,您只是想重新填充它。简单地删除它的逻辑。

foreach ($years as $year) {
    if (isset($_POST['year']) && $year == $_POST['year']) {
        $yearHTML .= "<option value='" . $year . "' selected='selected'>$year</option>" . PHP_EOL;
    } else {
        $yearHTML .= "<option value='" . $year . "'>$year</option>" . PHP_EOL;
    }
}

答案 2 :(得分:0)

问题是您要回发到服务器,服务器正在重新呈现页面。为了正确执行 并以适用于所有浏览器的方式,您需要将$ _POST结果(在正确转义它们之后)回显到输入元素。

同样,您需要使用选择框的$ _POST值来确定哪个option代码应设置selected="selected"属性。