Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Create custom day and time selection box in asp

Posted on 2013-01-25
1
Medium Priority
?
255 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 29

Accepted Solution

by:
sammySeltzer earned 2000 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

660 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