Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Dec 2005
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Sql query via a form button

    Hi,

    I have an area of web page that I do a query to display some
    information. It is basically a Select statement to display everything in the
    table, which loops around till everything is displayed. That works fine and
    dandy.
    I want to create three buttons to display information for "Past 24 Hours",
    "Past 72 Hours", and "All Entry's"

    I am not sure the best way to do this, any ideas?

    OH, the select statement for 24 hours, 72, or all entries I have but the
    part I am not sure is how to put the code into the buttons.

    thanks in advance,

  • #2
    Senior Coder
    Join Date
    Nov 2002
    Location
    North-East, UK
    Posts
    1,265
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You can use If/Else or Select Case to select the values
    http://www.asp101.com/samples/viewas...ile=case%2Easp

    Code:
    <%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
    <%
    If Len(Request.Form) > 0 Then
    
    iChoice = Request.Form("Submit")
    
    sql = "SELECT * FROM table"
    r_start_date = FormatDateTime(Now,2)
    
    	Select Case iChoice
    		Case "24"
    			r_end_date = FormatDateTime(Now-1,2)
    			sql = sql & " WHERE creation_time BETWEEN #" & r_start_date & "# AND #" & r_end_date & "#"
    		Case "72"
    			r_end_date = FormatDateTime(Now-3,2)
    			sql = sql & " WHERE creation_time BETWEEN #" & r_start_date & "# AND #" & r_end_date & "#"
    		Case Else
    			sql = sql
    	End Select
    End If
    %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    </head>
    
    <body>
    <form name="form1" id="form1" method="post" action="">
      <input type="submit" name="Submit" value="24" />
      <input type="submit" name="Submit" value="72" />
      <input type="submit" name="Submit" value="All" />
    </form>
    <%
    If sql <> "" Then
    	Response.Write sql
    End If
    %>
    </body>
    </html>

  • #3
    Senior Coder
    Join Date
    Dec 2002
    Location
    Arlington, Texas USA
    Posts
    1,072
    Thanks
    4
    Thanked 8 Times in 8 Posts
    if you use a submit button (input type="submit") it passes a value, so all you need to do is request.form the name of the button and grab that value.

    Example
    Code:
    <INPUT type="submit" value="Past 24 Hours" name="when">
    <INPUT type="submit" value="Past 72 Hours" name="when">
    <INPUT type="submit" value="All Entrys" name="when">
    
    
    dim someVar, whereClause
    someVar = Request.Form("when")
    If InStr(1,someVar,"24") Then 
         whereClause = " your 24 hour WHERE statement is defined here"
    ElseIf InStr(1,someVar,"72") Then
         whereClause = " your 72 hour WHERE statement is defined here" 
    Else
         whereClause = " your all WHERE statement is defined here"
    End If

  • #4
    New Coder
    Join Date
    Dec 2005
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up Solution....

    Hi Degsy & Miranda,

    I got it to work using the link the Degsy provded.

    Thanks again,

    Below is snippet of what I did. I am working on a intranet so I am retyping what I did.

    Later...

    A HREF="default.asp?choice=1">Past 24 Hours</A>&nbsp;
    <A HREF="default.asp?choice=2">Past 72 Hours</A>&nbsp;
    <A HREF="default.asp?choice=3">All items</A>
    <br>
    <%
    ' Declare our variable
    Dim iChoice

    ' Read in the choice the user clicked on.
    ' This will equal "" (an empty string) if no choice is there
    iChoice = Request.QueryString("choice")

    ' Execute the appropriate branch based upon the value of the variable we just read in.
    Select Case iChoice
    Case "1"
    %>
    Select * From Table Where ([Date] BETWEEN DateAdd('h',-24,Now()) AND Now())"
    <%
    Case "2"
    %>
    Select * From Table Where ([Date] BETWEEN DateAdd('h',-72,Now()) AND Now())" <%
    Case "3"
    %>
    "Select * From Table "
    <%
    Case Else
    ' This executes if no of the other conditions are met!
    ' This actually runs the first time through when the user
    ' arrives at this page because the choice is blank.

    "Select * From Table "

    End Select
    %>

  • #5
    Regular Coder
    Join Date
    Aug 2002
    Location
    USA
    Posts
    478
    Thanks
    0
    Thanked 2 Times in 2 Posts
    And for a related link for if you want everything to run off of one page:

    Classic ASP Design Tips - Post Back Page
    http://www.bullschmidt.com/devtip-postbackpage.asp
    J. Paul Schmidt
    www.Bullschmidt.com - Freelance Web and Database Developer
    www.Bullschmidt.com/DevTip.asp - Classic ASP Design Tips

  • #6
    New Coder
    Join Date
    Dec 2005
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi Bullschmidt,

    Actually the code that I ended up using that Degsy provided did run on one page.

    The link to your site also provided some nice suggestions.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •