使用PHP将下拉菜单粘贴

时间:2017-02-25 19:34:11

标签: php html

下面是我使用HTML和PHP制作的下拉菜单。我试图让菜单变得粘稠,但它似乎不起作用。关于我应该调整什么的任何想法?

<select name="month">
<option>Month</option>
<?php $months = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
     if($_POST['month'] != NULL) {
              echo "<option value='" . $_POST['month'] . "'>" . $_POST['month'] . "</option>";
      }
    foreach ($months as $month1){
        echo "<option value='$month1'>$month1</option>";
    }
?>
</select>

我尝试使用以下代码在提交表单之前选择了哪个月,在提交后保持选中状态:

if($_POST['month'] != NULL) {
              echo "<option value='" . $_POST['month'] . "'>" . $_POST['month'] . "</option>"; 

我认为在提交表单后,这将返回用户选择的任何月份。

谢谢!

1 个答案:

答案 0 :(得分:1)

如果您选择html标签的属性将是第一个选项而不考虑选项的位置,我对您的代码进行了点亮更改。

    <select name="month">
    <option>Month</option>
    <?php $months = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

        foreach ($months as $month1){
            if(isset($_POST['month']) && $_POST['month'] === $month1) {
                  echo "<option value='" . $month1 . "' selected >" . $month1 . "</option>";
            }else{
                echo "<option value='" . $month1 . "'>" . $month1 . "</option>";
            }

        }
?>
</select>