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 8961677
In Process

The Archive Base Latest Questions

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

Below I have a drop down menu where it will keep the display of

  • 0

Below I have a drop down menu where it will keep the display of the option selected in the drop down menu if there is a validation error in the form, else if the form has no errors then drop down menu will go back to displaying the “Please Select” option:

  $validSubmission = isset($_POST['registerbtn']) isset($getyear);

    $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 == $_POST['year'] && $numrows != 1) {
                $yearHTML .= "<option value='" . $year . "' selected='selected'>$year</option>" . PHP_EOL;
            } else {
                $yearHTML .= "<option value='" . $year . "'>$year</option>" . PHP_EOL;
            }
        }

    $yearHTML .= '</select>';

The values for the above drop down are:

1
2
3
4
5
...

10

But I am having problem using the same method with the drop down below as the drop down contains two types of options, those which contains a single number for a year, and those which displays multiple values for a year with a / in between as below:

1
1/2
2
2/3
3
3/4
4
4/5
5
....
10

My question is how can I use the format above for the drop down menu code below in order to keep the selected option selected if there is an error in the form, else if form has no errors then go back to displaying “Please Select” value in the drop down menu?

 $validSubmission = (isset($_POST['createbtn']) && isset($getcourseid) && isset($getcoursename) && isset($getduration));

$durationHTML = '';
$durationHTML .= '<select name="duration" id="durationDrop">'.PHP_EOL; 
$durationHTML .= '<option value="">Please Select</option>'.PHP_EOL;  

foreach ($years as $year) {
    $durationHTML .= "<option>$year</option>".PHP_EOL;  
    if ($year != $max_year) {
         $nextYear = $year + 1;
         $durationHTML .= "<option>$year/$nextYear</option>".PHP_EOL;              
    }
}
$durationHTML .= '</select>'; 
  • 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:56:58+00:00Added an answer on June 15, 2026 at 3:56 pm

    try it like this:

    foreach ($years as $year) 
    {
        $nextYear = $year + 1;
    
        //check for the "simple years"
        if ($validSubmission && $year == $_POST['duration'])
        {
            $durationHTML .= "<option selected>$year</option>".PHP_EOL;  
        }
        else
        {
            $durationHTML .= "<option>$year</option>".PHP_EOL; 
        }
    
        //check for the timespans
        if ($year != $max_year) 
        {
            $nextYear = $year + 1;
    
            if ($validSubmission && ($year . "/" . $nextYear) == $_POST['duration'])
            {
                $durationHTML .= "<option selected>$year/$nextYear</option>".PHP_EOL;    
            }
            else
            {
                $durationHTML .= "<option>$year/$nextYear</option>".PHP_EOL;  
            }
        }
    }
    

    Even though it is not tested, it should get you started. Hope it helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a drop down menu below: $form = <form action=' . htmlentities($_SERVER[PHP_SELF]) .
I have a page with a standard drop-down menu and a slider just below
Below I have a php script where it displays a Course drop down menu
Below I have a php script where it displays a Course drop down menu
I have a drop down box and some text input fields below it. Based
I would like to have a drop down menu with jQuery show and hide
In the below image my drop down menu is not displaying over the content.It
Ok i have this drop down menu script. It works fine when all the
I have got a drop down menu which works fine but i want to
I am getting this error only in chrome for superfish drop down menu. It

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.