Solved

Create custom day and time selection box in asp

Posted on 2013-01-25
1
238 Views
Last Modified: 2013-05-08
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

0
Comment
Question by:UName10
1 Comment
 
LVL 28

Accepted Solution

by:
sammySeltzer earned 500 total points
ID: 38825149
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

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now