Create custom day and time selection box in asp

I need to create drop down selects for the day (e.g. Monday) and Times (in hours & minutes) for the opening hours tab here on the advert page when an advert is created.

I'm adding new fields for the day and time in the database to the advert table so users can add times for every day of the week instead of free written.

My question is: how do I create the select option values for the day and time and send them to the fields in the table?

We have some code that currently handles the date and time and wondered if I could modify it, but wasn't sure how:

<%
Sub CreateSelectOption(byVal Value, byVal Display, byVal CurrentValue)
    %><option value="<% = Value %>" <% = GetSelectStatus_FromValues(Value,CurrentValue) %>><% = Display %></option><%
End Sub

Public Sub CreateDateTimeSelectionBox(strObjectName, dtCurrentDateTime, AllowNone)
    Dim iHour
    Dim iMin
    Dim i
    Dim strSelected
    if dtCurrentDateTime = "0" then
        dtCurrentDateTime = Now()
    end if
    If IsNull(dtCurrentDateTime) then
        iHour = ""
        iMin = ""
    Else
        dtCurrentDateTime = CDate(dtCurrentDateTime)
        iHour = Hour(dtCurrentDateTime)
        iMin = Minute(dtCurrentDateTime)
    End If
    CreateDateSelectionBox strObjectName, dtCurrentDateTime, AllowNone
    %>
    <select name="<% = strObjectName %>_Hour" class="input-list">
        <%
        If AllowNone = True then
            %><option value=""> - </option><%
        End if
        For i = 0 to 23
            if i = iHour then
                strSelected = " SELECTED "
            Else
                strSelected = ""
            end if
            %><option value="<% = Right("00" & Trim(CStr(i)), 2) %>" <% = strSelected %>><% = Right("00" & Trim(CStr(i)), 2) %></option><%
        Next
        %>
    </select>
    <select name="<% = strObjectName %>_Min" class="input-list">
        <%
        If AllowNone = True then
            %><option value=""> - </option><%
        End if
        For i = 0 to 59
            if i = iMin then
                strSelected = " SELECTED "
            Else
                strSelected = ""
            end if
            %><option value="<% = Right("00" & Trim(CStr(i)), 2) %>" <% = strSelected %>><% = Right("00" & Trim(CStr(i)), 2) %></option><%
        Next
        %>
    </select>
    <%
End Sub

Open in new window

UName10Asked:
Who is Participating?
 
sammySeltzerCommented:
If this was my task, I would store day, Month, Year into one table and Hour and minutes into another table. Then something like this would be very simple for you to you.


SQL = "SELECT TimeField FROM yourTable WHERE userid = " & someuserid
Set RS = conn.Execute( SQL )
when = RS("someTimeField")
hr = Hour( when )
mn = Minute( when )

Response.Write "<select name=""Hour"">" & vbNewLine
For h = 0 to 23 
    If h = hr Then sel = "selected" Else sel = ""
    Response.Write "<option value=""" & h & """ " & sel & ">" & Right("00" & h,2) & "</option>"
Next
Response.Write "</select>" & vbNewLine

Response.Write "<select name=""Minute"">" & vbNewLine
For m = 0 to 59
    If m = mn Then sel = "selected" Else sel = ""
    
    Response.Write "<option value=""" & m & """ " & sel & ">" & Right("00" & m,2) & "</option>"
Next
Response.Write "</select>" & vbNewLine

Open in new window



But, below is how you wanted it. Try this. I tested it and it works

Markup:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title> Hours Minutes Test</title>
<script type='text/javascript'>
  function field( id ) {
    var ele = document.getElementById( id )
    if ( !ele ) {
    }
    return ele
  }

  function populate() {
    var hours = field( 'hours' )
    if ( hours && hours.nodeName == 'SELECT' ) {
      for ( var h = 1; h < 13; h++ ) {
        hours.options[ hours.options.length++ ] = new option( h, h )
      }
    }
    var minutes = field( 'minutes' )
    if ( minutes && minutes.nodeName == 'SELECT' ) {
      for ( var m = 0; m < 60; m++ ) {
        minutes.options[ minutes.options.length++ ] = new option( m, m )
      }
    }
    var ampm = field( 'ampm' )
    if ( ampm && ampm.nodeName == 'SELECT' ) {
      ampm.options[ ampm.options.length++  ] = new option( 'AM', 'AM' )
      ampm.options[ ampm.options.length++  ] = new option( 'PM', 'PM' )
    }
  }

  window.onload = function() {
    populate()
    pick( 'time' )
  }

  function pick( id ) {
    function D2( val ) {
      return ( val < 10 ) ? '00' + val : val
    }
    var hours   = field( 'hours' )
    var minutes = field( 'minutes' )
    var ampm    = field( 'ampm' )
    var target  = field( id )
    var h       = hours.options[ hours.selectedIndex ].value
    var m       = minutes.options[ minutes.selectedIndex ].value
    var ap      = ampm.options[ ampm.selectedIndex ].value
  }
</script>
</head>
<body>
<form name="timetest" action="test.asp" method="post">
  <table>
   <tr>
    <td>
		 <select id=days name="days">
		  <option value="01">01</option>
		  <option value="02">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>
    </td>
   </tr>
   <tr>
    <td>
  <select id='hours' name='hours'></select>
  <select id='minutes' name='minutes'></select>
  <select id='ampm' name='ampm'></select>
  </td>
  </tr>
  </table>
  <input type="submit" value="Submit">
  </form>
</body>
</html>

Open in new window


Then test.asp

<%
days = request("days")
hours = request("hours")
minutes = request("minutes")

if len(minutes) =1 then
minutes = 0 & minutes
else
minutes = request("minutes")
end if
ampm = request("ampm")
times = hours & ":"&minutes & " " & ampm
%>

Open in new window


I just edited my original post
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.