Jump to content

Recommended Posts

Hi Guys

 

As you can see the code below displays a dropdwon datepicker.

For some reason that I can't figurre out the selected attribute on the MONTH field isn't working althou if I print $nowmonth I get the correct value.

 

DAY and YEAR work fine. can anyone see the problem? :facewall:

 

Cheers

 

 

<?php 
$nowday = date("d");
$nowmonth = date("m");
$nowyear = date("Y");
?>
<select name="arriveday" >

//************* FOLLOWING SELECT ATTRIBUTE GETS INSERTED CORRECTLY **********

<option value=01 <?php if ($nowday == 01) { echo"selected='selected'"; } ?>>01</option>
<option value=02 <?php if ($nowday == 02) { echo"selected='selected'"; } ?>>02</option>
<option value=03 <?php if ($nowday == 03) { echo"selected='selected'"; } ?>>03</option>
<option value=04 <?php if ($nowday == 04) { echo"selected='selected'"; } ?>>04</option>
<option value=05 <?php if ($nowday == 05) { echo"selected='selected'"; } ?>>05</option>
<option value=06 <?php if ($nowday == 07) { echo"selected='selected'"; } ?>>06</option>
<option value=07 <?php if ($nowday == 07) { echo"selected='selected'"; } ?>>07</option>
<option value=08 <?php if ($nowday == 08) { echo"selected='selected'"; } ?>>08</option>
<option value=09 <?php if ($nowday == 09) { echo"selected='selected'"; } ?>>09</option>
<option value=10 <?php if ($nowday == 10) { echo"selected='selected'"; } ?>>10</option>
<option value=11 <?php if ($nowday == 11) { echo"selected='selected'"; } ?>>11</option>
<option value=12 <?php if ($nowday == 12) { echo"selected='selected'"; } ?>>12</option>
<option value=13 <?php if ($nowday == 13) { echo"selected='selected'"; } ?>>13</option>
<option value=14 <?php if ($nowday == 14) { echo"selected='selected'"; } ?>>14</option>
<option value=15 <?php if ($nowday == 15) { echo"selected='selected'"; } ?>>15</option>
<option value=16 <?php if ($nowday == 16) { echo"selected='selected'"; } ?>>16</option>
<option value=17 <?php if ($nowday == 17) { echo"selected='selected'"; } ?>>17</option>
<option value=18 <?php if ($nowday == 18) { echo"selected='selected'"; } ?>>18</option>
<option value=19 <?php if ($nowday == 19) { echo"selected='selected'"; } ?>>19</option>
<option value=20 <?php if ($nowday == 20) { echo"selected='selected'"; } ?>>20</option>
<option value=21 <?php if ($nowday == 21) { echo"selected='selected'"; } ?>>21</option>
<option value=22 <?php if ($nowday == 22) { echo"selected='selected'"; } ?>>22</option>
<option value=23 <?php if ($nowday == 23) { echo"selected='selected'"; } ?>>23</option>
<option value=24 <?php if ($nowday == 24) { echo"selected='selected'"; } ?>>24</option>
<option value=25 <?php if ($nowday == 25) { echo"selected='selected'"; } ?>>25</option>
<option value=26 <?php if ($nowday == 26) { echo"selected='selected'"; } ?>>26</option>
<option value=27 <?php if ($nowday == 27) { echo"selected='selected'"; } ?>>27</option>
<option value=28 <?php if ($nowday == 28) { echo"selected='selected'"; } ?>>28</option>
<option value=29 <?php if ($nowday == 29) { echo"selected='selected'"; } ?>>29</option>
<option value=30 <?php if ($nowday == 30) { echo"selected='selected'"; } ?>>30</option>
<option value=31 <?php if ($nowday == 31) { echo"selected='selected'"; } ?>>31</option>
</select>

//************* FOLLOWING SELECT ATTRIBUTE DOESN'T GET INSERTED **********

<select name="arrivemonth">
<option value=01 <?php if ($nowmonth == 01) { echo"selected='selected'"; } ?>>January</option>

<option value=02 <?php if ($nowmonth == 02) { echo"selected='selected'"; } ?>>February</option>
<option value=03 <?php if ($nowmonth == 03) { echo"selected='selected'"; } ?>>March</option>
<option value=04 <?php if ($nowmonth == 04) { echo"selected='selected'"; } ?>>April</option>
<option value=05 <?php if ($nowmonth == 05) { echo"selected='selected'"; } ?>>May</option>
<option value=06 <?php if ($nowmonth == 06) { echo"selected='selected'"; } ?>>June</option>
<option value=07 <?php if ($nowmonth == 07) { echo"selected='selected'"; } ?>>July</option>

<option value=08 <?php if ($nowmonth == 08) { echo"selected='selected'"; } ?>>August</option>
<option value=09 <?php if ($nowmonth == 09) { echo"selected='selected'"; } ?>>September</option>
<option value=10 <?php if ($nowmonth == 10) { echo"selected='selected'"; } ?>>October</option>
<option value=11 <?php if ($nowmonth == 11) { echo"selected='selected'"; } ?>>November</option>
<option value=12 <?php if ($nowmonth == 12) { echo"selected='selected'"; } ?>>December</option>
</select>

//************* FOLLOWING SELECT ATTRIBUTE GETS INSERTED CORRECTLY **********
<select name="arriveyear">
<option value=2009 <?php if ($nowyear == 2009) { echo"selected='selected'"; } ?>>2009</option>
<option value=2010 <?php if ($nowyear == 2010) { echo"selected='selected'"; } ?>>2010</option>
</select>

Link to comment
https://forums.phpfreaks.com/topic/169745-solved-date-dropdown-selected-issue/
Share on other sites

You can easily cut out all that unnecessary duplicate code with a loop.

<select name="arriveday">
<?php
for($x = 1; $x <= 31; $x++) {
$dayNum = str_pad($x, 2, "0", STR_PAD_LEFT); 
print "<option value='".$dayNum."'".((date("d") == $dayNum) ? " selected" : false).">".$dayNum."</option>\n";
}
?>
</select>

Because your HTML is bad. Enclose values in quotes. Also 01,02, etc is not an integer in php.

<option value=01 <?php if ($nowday == 01) { echo"selected='selected'"; } ?>>01</option>

Corrected

<option value="01" <?php if ($nowday == "01") { echo "selected"; } ?>>01</option>

 

Please use a loop. Writing it out like that looks awful.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.