Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8959969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:33:59+00:00 2026-06-15T15:33:59+00:00

let me just first say that I have tried to cut the code below

  • 0

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'];
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:34:00+00:00Added an answer on June 15, 2026 at 3:34 pm

    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;
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First, let me just say that I have searched for information on this topic;
First of all, let me just say that I'm using the PHP framework Yii,
Let me first put the code snippets here. I am just using the ASP.NET
Let's just say that I need to render a certain amount of HTML over
I've been having this bothering recurring theme; let's just say, I have a class
Let's say I have an XML document that has this: <keywords> <keyword>test</keyword> <keyword>test2</keyword> <keyword>test3</keyword>
I am using EntityFramework 4.3.1 and Database first with dbcontext. Let's say I have
Let's say I have the following Python code: if conditionOne(): if conditionTwo(): foo =
I have a problem here with the php function substr Let's say I have
Let say that I have an interface for inputting vehicle marketing information +--------------------+ |

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.