錯誤消息「請選擇」選項,讓我先說,我試圖削減下面小的代碼可能:如何停止下拉菜單顯示在顯示
<?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'];
你的代碼中包含邏輯的很多,是非常難追(更不用說不方便,當有人有不止一個錯誤,因爲它只會顯示其中之一,他們將不得不再次提交表單)。當我驗證表單時,我將所有錯誤添加到數組中,然後循環遍歷它們以將其顯示回給用戶。 – Mike
@Mike是的,這是我可以做出的一個改進。說實話,我正在做後端,所以是否由我來確定,還是由做前端的人決定? – user1881090