它可能在客户端做得更好,但既然你想用 PHP 来做,这里有一个使用关联数组的简单解决方案:
一些文件.php:
<?php
$months = array(1 => 'January', 2 => 'February', 3 => 'March', 4 => 'April', 5 => 'May',
6 => 'June', 7 => 'July', 8 => 'August', 9 => 'September', 10 => 'October', 11 => 'November',
12 => 'December');
if(isset($_POST['sb'])){
$input = $_POST['month'];
echo "You selected ".$months[$input];
}
?>
<form action="" method="post">
<select name="month">
<option value="1">January</option>
<option value="2">Febuary</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<input type="submit" name="sb"/>
</form>
如果你想动态生成你的菜单,你可以这样做:
<?php
$curr_month = date("m");
$month = array (1=>"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
$select = "<select name=\"month\">\n";
foreach ($month as $key => $val) {
$select .= "\t<option val=\"".$key."\"";
if ($key == $curr_month) {
$select .= " selected=\"selected\">".$val."</option>\n";
} else {
$select .= ">".$val."</option>\n";
}
}
$select .= "</select>";
echo $select;
?>
希望这可以帮助!