<?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 = "";
}
然后我想让下拉菜单返回“请选择”选项。如何实现这一目标?
答案 0 :(得分:1)
在构建select
的HTML之前,您必须执行查询。然后你可以改变:
if ($validSubmission && $year == $getyear) {
要:
if ($validSubmission && $year == $getyear && $numrows != 1) {
然而,尽管进攻很少,但我认为您的代码需要进行一些重构。我看到的第一件事是嵌套if
验证输入的大量缩进。你可能想试着把它弄平一点。您甚至可以一次显示所有可检测到的错误,以便用户可以更正所有错误,然后然后重新提交,而不是修复,重新提交,修复另一个......
我还注意到你将很多HTML嵌入到字符串中。这确实没有必要。您可以简单地结束PHP块并在那里启动HTML,而不是执行巨大的$form = ...; echo $form;
事务。然后当你到达:
" . 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;