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
Solved

Create custom day and time selection box in asp

Posted on 2013-01-25
1
245 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

829 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