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

  • SEARCH
  • Home
  • 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 8951583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:41:51+00:00 2026-06-15T13:41:51+00:00

<?php // required variables (make them explciit no need for foreach loop) $getyear =

  • 0
<?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?

  • 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-15T13:41:52+00:00Added an answer on June 15, 2026 at 1:41 pm

    You’ll have to perform your query before you construct the HTML for the select. Then you can change:

    if ($validSubmission && $year == $getyear) {
    

    To:

    if ($validSubmission && $year == $getyear && $numrows != 1) {
    

    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:

    " . htmlentities($_SERVER["PHP_SELF"]) . "
    

    you could simply use:

    <?php echo htmlentities($_SERVER("PHP_SELF"]); ?>
    

    and then go straight back into the HTML. Similarly, instead of {$yearHTML}, you could use:

    <?php echo $yearHTML; ?>
    

    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 if statements 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:

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

Sidebar

Related Questions

I've written this short piece of php code that requires 2 variables name and
Is there a keyboard shortcut or plugin to open PHP required or included files
I've reviewed a couple options for jquery-based RTE that required PHP. I'm running a
I'm getting PHP 5.2+ required when installing OpenCart on Bluehost. Bluehost runs PHP 5.2.9.
I'm going to pass through some values between two required/included php file,ex: mysql.php: <?php
I am required to work on a php project that requires the database to
Ok, I couldn't insert the overview image, but I'm required to create a PHP
I'm having trouble with global variables in php. I have a $screen var set
My Website is Structured like: index.php which includes a router.php and router.php calls required
Happy New Year Everyone! I have this existing php code I did when I

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.