0
<?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']) && isset($getyear) && isset($getpass) && isset($getretypepass));

            $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 && $year == $getyear) {
            $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;

在上面的代码中,我设法在提交表单后保持下拉选项保持选中状态。但我想要做的是,如果提交表单后出现成功消息:

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

 $getyear = "";


}

然后我希望下拉菜单返回“请选择”选项。如何做到这一点?

4

1 回答 1

1

在为select. 然后你可以改变:

if ($validSubmission && $year == $getyear) {

到:

if ($validSubmission && $year == $getyear && $numrows != 1) {

但是,尽可能少冒犯,我认为您的代码需要一些重构。if我看到的第一件事是嵌套s 验证输入的大量缩进。你可能想试着把它弄平一点。您甚至可以一次显示所有可检测到的错误,以便用户可以更正所有错误然后重新提交,而不是修复一个、重新提交、修复另一个......

我还注意到您将大量 HTML 嵌入到字符串中。这确实是没有必要的。与其做大事,不如$form = ...; echo $form;简单地结束 PHP 块并在那里开始 HTML。然后当你到达:

" . htmlentities($_SERVER["PHP_SELF"]) . "

你可以简单地使用:

<?php echo htmlentities($_SERVER("PHP_SELF"]); ?>

然后直接回到 HTML。同样,{$yearHTML}您可以使用:

<?php echo $yearHTML; ?>

但是,为什么还要在字符串中构建年份 HTML?只需在那里输出选项。


附录:一次显示多个错误

正如我之前提到的,您使用一堆嵌套if语句来管理错误。我想这行得通,但这不是一种特别好的做事方式。一个更好的方法来做的事情也恰好允许你显示多个错误是这样的:

// If we're dealing with multiple errors, we'll need an array to hold them.
$errors = array();

// Then we can go along down the conditions, checking each one.
// If there's an error, we can add it to the array.
if(!$getyear) {
    $errors[] = "You must provide a year.";
}

// If detecting one error requires another to have passed successfully,
// you can either add another condition or nest if statements --
// but don't nest unnecessarily.
if(!$getpass) {
    $errors[] = "You must provide a password.";
}else if(strlen($getpass) < 6) {
    $errors[] = "Your password must be at least 6 characters long.";
}else if($getpass !== $getretypepass) {
    $errors[] = "Your passwords do not match.";
}

// When you come to a point where you need everything to be valid to complete
// an operation, just check to see if you've accumulated any errors:
if(!$errors) {
    // Fetch stuff from your database or whatever...

    // Now more error checking. For example, checking for duplicate usernames.
}

// If you needed to do another operation after that error checking,
// it doesn't need to be nested into that last if statement;
// just put it in a new one down here.

// After all is said and done, you can display the errors if there were any:
if($errors): ?>
    <ul class="errors">
    <?php foreach($errors as $error): ?>
        <li><?php echo $error; ?></li>
        <?php /* don't forget to escape the errors
                 if they contain user-provided input */ ?>
    <?php endforeach; ?>
    </ul>
<?php else: ?>
    <p>Hooray! No errors!</p>
<?php endif;
于 2012-12-08T03:17:35.430 回答