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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:53:58+00:00 2026-06-14T16:53:58+00:00

I have a drop down menu below: $form = <form action=’ . htmlentities($_SERVER[PHP_SELF]) .

  • 0

I have a drop down menu below:

  $form = "
<form action='" . htmlentities($_SERVER["PHP_SELF"]) . "' method='post'>
  <table>
  <tr>
  <td><select name='year' id='yearDrop'>
<option value=''></option>
<option value='$getyear[1]'>1</option>
<option value='$getyear[2]'>2</option>
<option value='$getyear[3]'>3</option>
<option value='$getyear[4]'>4</option>
<option value='$getyear[5]'>5</option>
<option value='$getyear[6]'>6</option>
<option value='$getyear[7]'>7</option>
<option value='$getyear[8]'>8</option>
<option value='$getyear[9]'>9</option>
<option value='$getyear[10]'>10</option>
</select></td>
</tr>
</table>
</form>
";

Notice the drop down menus are in a php varaible in a form. My question is: “is there a much better and shorter method of displaying the drop down menu above?”. Maybe by using some sort of loop to loop through each value and give each option the same value attribute?

I want to use getyear in order to fit in a simple if statement:

    if ($getyear){
    echo "year is chosen";
    }else{
    $errormsg = "You must enter in Student's current Academic Year to Register";
              }

UPDATE:

<?php  

  $getyear = (isset($_POST['year'])) ? $_POST['year'] : '';
  $errormsg = (isset($errormsg)) ? $errormsg : '';

$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) {
    $yearHTML .= "<option>$year</option>".PHP_EOL;  // if no value attribute, value will be whatever is inside option tag, in this case, $year
}
$yearHTML .= '</select>'; 

  if( (isset($_POST['registerbtn']))){
      $getyear = $_POST['year'];

      if (!in_array($getyear , $years)){
      echo "year is chosen";
                    }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>
  <td><input type='text' name='year' value='$getyear' /></td>
  </tr>
  <tr>
  <td></td>
  <td><input type='submit' value='Register' name='registerbtn' /></td>
  </tr>
  </table>
  </form>";

  echo $form;

?>
  • 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-14T16:53:59+00:00Added an answer on June 14, 2026 at 4:53 pm

    The foreach loop is extremely beneficial when looping over arrays and performing operations on their values.

    foreach control structure

    <?php
    // reference values for output and for validation later
    $min_year = 1900;  // using as an example
    $max_year = 2012;
    $years = range($min_year, $max_year); // returns array with numeric values of 1900 - 2012
    
    // for HTML output
    if (empty($_POST)) {  // display form
        $html = '';
        foreach ($years as $year) {
            $html .= "<option>$year</option>";  // if no value attribute, value will be whatever is inside option tag, in this case, $year
        }
        // output HTML
        echo $html;
    } else {  // process form
        $chosen_year = isset($_POST['year']) ? $_POST['year'] : "";
        if (!in_array($chosen_year , $years) {  // year is invalid
            $errormsg = "You must enter in Student's current Academic Year to Register";
        } else {  // year is chosen
            echo 'year is chosen';
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a page with a standard drop-down menu and a slider just below
I have a css drop down menu (below), however, my problem is this, once
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 menu which you can see here on jsfiddle .
I have a drop down menu. Has product types. Product types have associated value
I have a drop down menu, that a user selects a criteria from, based
I have a drop down menu which fills with some doc names. Now when
Currently have a drop down menu that is activated on a hover (from display:none
I have a drop down menu in an ASPX page along the lines of:

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.