以编程方式生成下拉菜单

时间:2012-11-22 01:50:57

标签: php html

我在下面有一个下拉菜单:

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

请注意,下拉菜单是在表单中的php变量中。我的问题是:“有没有更好,更短的方法显示上面的下拉菜单?”。也许通过使用某种循环来遍历每个值并为每个选项提供相同的value属性?

我想使用getyear来填充简单的if语句:

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

更新

<?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;

?>

2 个答案:

答案 0 :(得分:1)

当循环遍历数组并对其值执行操作时,foreach循环非常有用。

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';
    }
}

答案 1 :(得分:0)

此文件将自动生成select选项,为您提供进行进一步处理的便捷位置。     

function process_form(){
    if(!isset($_POST['year']) || !is_numeric($_POST['year'])){
        // Form invalid
        return false;
    }

    $year   = $_POST['year'];

    // Perform actions - save to database, etc..?

    // Form valid
    return true;
}

$error  = false;
if($_SERVER['REQUEST_METHOD'] == 'POST'){
    // Form submitted
    $result = process_form();
    if($result){
        ?>
        <p>Valid year selected</p>
        <?php
        exit;
    }

    $error  = true;
}
?>
<form action="" method="post">
<?php if($error){ ?>
    <p class="error">You must select the student's current academic year to register</p>
<?php } ?>
<label for="select_year">Academic Year:</label>
<select name="year" id="select_year">
    <option value=""></option>
    <?php for($year = $start_year; $year <= $end_year; $year++){ ?>
        <option value="<?php echo $year;?>"><?php echo $year;?></option>
    <?php } ?>
</select>

<button type="submit">Register</button>
</form>