以下是我的代码和结构。问题是提交表单时,我无法保留下拉框的值。有人可以强调我哪里出错了吗?或者,如果有更好的方法来处理这个问题,请提出建议。谢谢你。
注意:我尝试在以下代码中仅将上述应用应用于一个下拉菜单,但我会将相同的内容扩展到两个下拉菜单。
代码 :
<!doctype html>
<html>
<head></head>
<body>
<form name="form1" method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>" id="form1">
<div>
<div="pickYear">
<select name="yearPickNow">
<option value='selectyear'<?php echo isset($_GET["yearPickNow"]) && $_GET["yearPickNow"] == "selectyear" ? "selected" : "" ?>>Year</option>
<option value="2014" <?php if($_POST['yearPickNow'] == '2014') {echo "selected='selected'"; } ?> >2014</option>
<option value='2015'<?php echo isset($_GET["yearPickNow"]) && $_GET["yearPickNow"] == "2015" ? "selected" : "2015" ?>>2015</option>
<option value='2016'<?php echo isset($_GET["yearPickNow"]) && $_GET["yearPickNow"] == "2016" ? "selected" : "2016" ?>>2016</option>
<option value='2017'<?php echo isset($_GET["yearPickNow"]) && $_GET["yearPickNow"] == "2017" ? "selected" : "2017" ?>>2017</option>
</select>
</div>
<div id="pickMonth">
<select name="monthPickNow" id="monthPickNow" onChange="document.forms['form1'].submit()">
<option value='selectmonth'>Month</option>
<option value='January'>January</option>
<option value='February'>February</option>
<option value='March'>March</option>
</select>
</div>
</div>
<?php
//some php code to bring up and display content relevant to the selected year and month from a SQL database
?>
</form>
</body>