Skip to content
Advertisement

HTML select option selected does not work

I want to set the current date by default but it works only on year’s select (the last one).

I wouldn’t like to use javascript in this case. Just html and php.

I have already tried “selected=’selected'” and “autocomplete=off” as shown in different other posts.

Here my code

<form id="form-orders-date" onsubmit="selectOrdersDate();">
                    <select id="orders-day">
                        <?php 
                        for ($i=1; $i <= 31; $i++) {
                            if (sprintf('%02d', $i) == date("d")) {
                                echo "<option value="".sprintf('%02d', $i)."" selected>".sprintf('%02d', $i)."</option>";
                            }
                            else{
                                echo "<option value="".sprintf('%02d', $i)."">".sprintf('%02d', $i)."</option>" ;
                            }
                        } 
                        ?>
                    </select>
                    <select id="orders-month">
                        <?php 
                        for ($i=1; $i <= 12; $i++) {
                            if (sprintf('%02d', $i) == date("m")) {
                                echo "<option value="".sprintf('%02d', $i)."" selected>".sprintf('%02d', $i)."</option>";
                            }
                            else{
                                echo "<option value="".sprintf('%02d', $i)."">".sprintf('%02d', $i)."</option>" ;
                            }
                        } 
                        ?>
                    </select>
                    <select id="orders-year">
                        <?php 
                        for ($i=2012; $i <= 2020; $i++) {
                            if (sprintf('%02d', $i) == date("Y")) {
                                echo "<option value="".sprintf('%02d', $i)."" selected>".sprintf('%02d', $i)."</option>";
                            }
                            else{
                                echo "<option value="".sprintf('%02d', $i)."">".sprintf('%02d', $i)."</option>" ;
                            }
                        } 
                        ?>
                    </select>
                    <input type="submit" value="OK" />
</form>

here the plain HTML when the PHP has been run:

<form id="form-orders-date" onsubmit="selectOrdersDate();">
    <select id="orders-day">
        <option value="01">01</option>
        <option value="02" selected>02</option>
        <option value="03">03</option>
        <option value="04">04</option>
        <option value="05">05</option>
        <option value="06">06</option>
        <option value="07">07</option>
        <option value="08">08</option>
        <option value="09">09</option>
        <option value="10">10</option>
        <option value="11">11</option>
        <option value="12">12</option>
        <option value="13">13</option>
        <option value="14">14</option>
        <option value="15">15</option>
        <option value="16">16</option>
        <option value="17">17</option>
        <option value="18">18</option>
        <option value="19">19</option>
        <option value="20">20</option>
        <option value="21">21</option>
        <option value="22">22</option>
        <option value="23">23</option>
        <option value="24">24</option>
        <option value="25">25</option>
        <option value="26">26</option>
        <option value="27">27</option>
        <option value="28">28</option>
        <option value="29">29</option>
        <option value="30">30</option>
        <option value="31">31</option>
    </select>
    <select id="orders-month">
        <option value="01">01</option>
        <option value="02">02</option>
        <option value="03" selected>03</option>
        <option value="04">04</option>
        <option value="05">05</option>
        <option value="06">06</option>
        <option value="07">07</option>
        <option value="08">08</option>
        <option value="09">09</option>
        <option value="10">10</option>
        <option value="11">11</option>
        <option value="12">12</option>
    </select>
    <select id="orders-year">
        <option value="2012">2012</option>
        <option value="2013">2013</option>
        <option value="2014">2014</option>
        <option value="2015">2015</option>
        <option value="2016">2016</option>
        <option value="2017" selected>2017</option>
        <option value="2018">2018</option>
        <option value="2019">2019</option>
        <option value="2020">2020</option>
    </select>
    <input type="submit" value="OK" />
</form>

Advertisement

Answer

This is the code you are looking for Remember to use single quotes (‘) when including double quotes inside the PHP echo or else if using double quotes use ” inside the outer double quotes.

<form id="form-orders-date" onsubmit="selectOrdersDate();">
    <select id="orders-day">
      <?php
      for ($i=1; $i <= 31; $i++) {
        if ($i == date("d")) {
          echo '<option value="'.$i.'" selected>'.$i.'</option>';
        }
        else{
          echo '<option value="'.$i.'">'.$i.'</option>';
        }
      }
      ?>
    </select>
    <select id="orders-month">
      <?php
      for ($i=1; $i <= 12; $i++) {
        if ($i == date("m")) {
          echo '<option value="'.$i.'" selected>'.$i.'</option>';
        }
        else{
          echo '<option value="'.$i.'">'.$i.'</option>';
        }
      }
      ?>
    </select>
    <select id="orders-year">
      <?php
      for ($i=2012; $i <= 2020; $i++) {
        if ($i == date("Y")) {
          echo '<option value="'.$i.'" selected>'.$i.'</option>';
        }
        else{
          echo '<option value="'.$i.'">'.$i.'</option>';
        }
      }
      ?>
    </select>
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement