Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop drop down menu displaying "Please Select" option when error message is displayed [closed]

Tags:

html

php

let me just first say that I have tried to cut the code below as small as possible:

        <?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 this is the issue I am having. I have a drop down menu for years and two text inputs for typing in password and re type password. Now if the user selects a year from the drop down menu and submits the form, then it displays the error message stating a password must be entered and the year drop down menu still displays the option the user has selected from the year drop down menu.

The problem I am having is that if either of these message below appear, then the drop down option goes back to displaying "Please Select". My question is that how can I stop the drop down menu going back to the "Please Select" option when the following error messages appear?

$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";

The line of code I believe needs changing is here:

    $validSubmission = isset($_POST['registerbtn']) && $_POST['year'] && $_POST['studentpass'] && $_POST['retypepass'];
like image 861
user1881090 Avatar asked Nov 03 '22 09:11

user1881090


2 Answers

I think what you're looking for is this:

// This is just a suggestion, but I would set these to null instead of an empty string:
    $getyear       = (isset($_POST['year'])) ? $_POST['year'] : null;
    $getpass       = (isset($_POST['studentpass'])) ? $_POST['studentpass'] : null;
    $getretypepass = (isset($_POST['retypepass'])) ? $_POST['retypepass'] : null;
    $errormsg      = (isset($errormsg)) ? $errormsg : null;
// You have already defined these, so no need to use $_POST here
// Also, I like to compare it to an actual value rather than just doing if ($variable)
// to avoid unforseen circumstances. Note, isset() returns false if $var == null.
$validSubmission = (isset($_POST['registerbtn']) && isset($getyear) && isset($getpass) && isset($getretypepass));

Then I think you've got your logic backwards later on:

    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;
        }
    }
like image 112
Mike Avatar answered Nov 14 '22 16:11

Mike


When repopulating the Year dropdown, you probably don't actually care if any part of the submission is valid or not, you just want to repopulate it regardless. Simply drop the logic for that.

foreach ($years as $year) {
    if (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;
    }
}
like image 31
Wing Lian Avatar answered Nov 14 '22 15:11

Wing Lian