Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Default date in select options 1

Status
Not open for further replies.

dom24

Programmer
Aug 5, 2004
218
0
0
GB
Hi,

I'm creating a room booking system and so far I have 3 drop down menus at the top of the screen where the user can select the day, month and year.
I have hard coded the dates so that they can be written to the database when required, rather than add every date to the database and pull them back.

I want the date to display the current date when the user opens the page. Does anyone know how I can set the Selected Values of the Option so that is will use today's day, month and year?

Thanks.
 
My suggestion would be to either not hardcode the dates or to simply add extra options at the top of each box (as first options they will be selected by default) with today's day/month/year in them.
Code:
[b]Build em all dynamically[/b]
Dim i, d_options, m_options, y_options

For i = 1 to 31
   d_options = d_options & "<option"
   If Day(Now()) = i Then d_option = d_options & " selected"
   d_options = d_options & >" & i & "</option>"
Next

For i = 1 to 12
   m_options = m_options & "<option value=""" & i & """"
   If Month(Now()) = i Then m_option = m_options & " selected"
   m_options = m_options & >" & MonthName(i) & "</option>"
Next

For i = Year(Now()) to Year(Now()) + 5
   y_options = y_options & "<option>" & i & "</option>"
Next

[b]Adding dynamic dates to static options[/b]
<select name="selDay">
   <option><%=Day(Now())%></option>
   <option>1</option>
   <option>2</option>
   <option>3</option>
   <option>4</option>
   <option>5</option>
   <option>6</option>
   ...etc
</select>

<select name="SelMonth">
   <option value="<%=Month(Now())%>"><%=MonthName(Month(Now()))%></option>
   <option value="1">January</option>
   ...etc

There are two other options I have thought of, both using javascript. The first is to use an actual javascript call in the body onLoad event to find the correct day/month/year and select them all client-side. The secon is a combination that would use ASP to get the day/month/year and then javascript to select them:
Code:
<html>
<head>
<script language="javascript">
function scrollDate(dy, mnth, yr){
   document.getElementById("daySelectId").selectedIndex = dy - 1;
   document.getElementById("monthSelectId").selectedIndex = mnth - 1;
   document.getElementById("yearSelectId").selectedIndex = yr;
}
</script>
</head>
<body onLoad="scrollDate(<%=Day(Now())%>,<%=Month(Now())%>,<%=Year(Now()) - 2005%>);">
<select name="selDay" id="daySelectId">
   <option>1</option>
   <option>2</option>
   ...etc
</select>
<select name="selMonth" id="monthSelectId">
   <option value="1">January</option>
   <option value="2">February</option>
   ...etc
</select>
<select name="selYear" id="yearSelectId">
   <option>2005</option>
   <option>2006</option>
   ...etc
</select>
Basically this example ought (cross fingers) to change the selected index to the correct indexes for day, month, and year. This is just an example though, so it may have issues that naturally occur when writing code on the fly at 5pm on a Friday :p

Hope this post helped,

-T

barcode_1.gif
 
That's a great help Tarwn thanks.
I'm not too hot on Javascript though so i'm gonna stick to your suggestion of building them dynamically.
I've used what you said and create the drop downs aroudn it but i'm not getting any values in them?
Any ideas why not?
 
Double check that a) the names are what you are asking for in the next page and b) that they are inside the form tags (otherwise they won't get passed).

-T

barcode_1.gif
 
No I mean on the first page where I'm creating the option list there are no values in the list to be selected.
The lists are just empty.
 
Never mind. Just me being thick :0)
You're a great help, thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top