niech mi tylko najpierw powiedzieć, że próbowali wyciąć poniższy kod tak małe jak to możliwe:Jak zatrzymać rozwijanego menu wyświetlania „Wybierz opcję” gdy wyświetlany jest komunikat o błędzie
<?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;
Ok to jest kwestia, którą mam. Mam rozwijane menu od lat i dwa wejścia tekstowe do wpisywania hasła i ponownego wpisywania hasła. Teraz, jeśli użytkownik wybierze rok z rozwijanego menu i prześle formularz, wyświetli komunikat o błędzie z informacją, że należy wprowadzić hasło, a menu rozwijane roku nadal wyświetla opcję wybraną przez użytkownika z rozwijanego menu roku.
Problem polega na tym, że jeśli pojawi się jeden z poniższych komunikatów, opcja rozwijana wraca do wyświetlania "Wybierz proszę". Moje pytanie brzmi: jak mogę zatrzymać rozwijane menu wracając do opcji "Proszę wybrać", gdy pojawią się następujące komunikaty o błędach?
$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";
Linia kodu wierzę potrzeby zmieniania jest tutaj:
$validSubmission = isset($_POST['registerbtn']) && $_POST['year'] && $_POST['studentpass'] && $_POST['retypepass'];
Kod zawiera wiele logiki, która jest niezwykle trudne do naśladowania (nie wspominając o niewygodne, gdy ktoś ma więcej niż jeden błąd, ponieważ będzie tylko pokazać jedną z nich i będą musieli ponownie złożyć formularz). Kiedy sprawdzam poprawność formularzy, dodajemy wszystkie moje błędy do tablicy, a następnie przeglądam je, aby wyświetlić je użytkownikowi. – Mike
@Mike Tak, to jest jedno z ulepszeń, które mogę zrobić. Szczerze mówiąc, robię z powrotem koniec, więc czy to zależy od mnie, czy to do osoby prowadzącej front end? – user1881090