Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Kal
    Kal is offline
    Regular Coder
    Join Date
    Dec 2005
    Posts
    309
    Thanks
    0
    Thanked 0 Times in 0 Posts

    month year drop down

    Hi Guys

    The following code originally posted by raf works great, howeveri need some help modifying it slightly.

    is there away in which it could display all 12 months for the current year? but the default should remain on the current month.

    at the moment it only displays the upcoming 5 months. and not the previous months already gone in the year.

    thanks in advance

    Code:
    <select name="month">
    							<%
    							Dim i2, value, optionv
    							i2 = 0
    							do while i2 <= 5
    							value = Month(DateAdd("m",i2, Now()))
    							optionv = MonthName(value) + " " +  Right(Year(Now),2)
    							response.write("<option value='" & optionv & "'>"& optionv &"</option>")
    							i2= i2+1
    							loop
    							%>
    						</select>

  • #2
    Senior Coder TheShaner's Avatar
    Join Date
    Sep 2005
    Location
    Orlando, FL
    Posts
    1,126
    Thanks
    2
    Thanked 40 Times in 40 Posts
    Code:
    <select name="month">
    <%
      Dim i, cur_month, optionv, sel_text
      sel_text = ""    'Display whether the month is selected
      cur_month= Month(Now) 'Current Month's integer
      For i = 1 To 12
        If (i = cur_month) Then sel_text = " selected='selected'" 
        optionv = MonthName(i) & " " &  Right(Year(Now), 2)
        Response.Write("<option value='" & optionv & "'" & sel_text & ">" & optionv & "</option>")
      Next
    %>
    </select>
    The above code is just looping through all 12 months and displaying them. If during the loop it comes to a month that is equal to the current month's integer, then it'll display the text to make sure that month is selected by default in the drop down.

    -Shane

  • #3
    Kal
    Kal is offline
    Regular Coder
    Join Date
    Dec 2005
    Posts
    309
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks for your reply just tested your code and it seems to default to December 07 rather than May 07.

  • #4
    Senior Coder TheShaner's Avatar
    Join Date
    Sep 2005
    Location
    Orlando, FL
    Posts
    1,126
    Thanks
    2
    Thanked 40 Times in 40 Posts
    My fault! What was happening was that once it got to May, although it set the text to display the selected='selected' in the May 07 option, it never reset it back to nothing and so every month after it also received selected='selected'. If you look at the source code, you'll see. I tested the below code and it works. The red text indicates the changed code.
    Code:
    <select name="month">
    <%
      Dim i, cur_month, optionv, sel_text
      sel_text = ""    'Display whether the month is selected
      cur_month= Month(Now) 'Current Month's integer
      For i = 1 To 12
        If (i = cur_month) Then
          sel_text = " selected='selected'" 
        Else
          sel_text = ""
        End If
        optionv = MonthName(i) & " " &  Right(Year(Now), 2)
        Response.Write("<option value='" & optionv & "'" & sel_text & ">" & optionv & "</option>")
      Next
    %>
    </select>
    -Shane

  • #5
    Kal
    Kal is offline
    Regular Coder
    Join Date
    Dec 2005
    Posts
    309
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks shane works great. i looked at the source code and see what you mean.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •