<?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;
In the code above, I have managed to keep a drop down option stay selected after the form hs been submitted. But what I want to do is that if the success message appear after form is submitted:
if ($numrows == 1) {
$errormsg = "<span style='color: green'>Student has been Registered</span>";
$getyear = "";
}
Then I want the drop down menu to go back the “Please Select” option. How can this be achieved?
You’ll have to perform your query before you construct the HTML for the
select. Then you can change:To:
With as little offense as possible, however, I think your code needs some refactoring. The first thing I see is the huge amount of indentation from the nested
ifs validating the inputs. You may want to try to flatten that a little. You could even show all the detectable errors at once so the user can correct all of them and then resubmit rather than fixing one, resubmitting, fixing another…I also notice you’re embedding a lot of HTML into strings. This, truly, is not necessary. Rather than doing your huge
$form = ...; echo $form;thing, you could simply end the PHP block and start the HTML there. Then when you get to:you could simply use:
and then go straight back into the HTML. Similarly, instead of
{$yearHTML}, you could use:However, why even build up the year HTML in a string? Just output the options there.
Addendum: Showing multiple errors at once
As is, as I’ve mentioned before, you use a bunch of nested
ifstatements to manage errors. I guess that works, but it’s not a particularly nice way to do things. A nicer way to do things which also happens to allow you to show multiple errors would be something like this: