Drop down question

T

Tom

Hello
In this sample code there is a drop down box that is populated with values
from the database. What is the right method to capture this user selected
value and to use that value as the filter for another drop down box or even
as a value to be processed by by the page?

Thanks
Tom

<%
'************************************
'Subroutine for droplist box
'
'PURPOSE: code to be called for placement of droplist box in a form or page
'
'PARAMETERS:
'strSQL = Defines SQL statement
'strFieldName = Defines field name in database table
'strDefault = Defines default value in droplist
'StrBoxName = Defines name of droplistbox
'strBoxTitle = Defines the title shown next to droplist box on screen
'strConn = Defines the database connection string
'*************************************

Sub Droplist(strSQL,strFieldName,strDefault,StrBoxName,strBoxTitle,strConn)

'Set Cursor
'-------------------------------------------------------------------------
Const adOpenStatic=3

' create the recordset, open it, and move to first record
'-------------------------------------------------------------------------

Set rs = Server.CreateObject ("ADODB.Recordset")
rs.Open strSQL, strConn,adOpenStatic

rs.movefirst

'Ouput result to droplist box
'-------------------------------------------------------------------------

strBoxTitle%>
<SELECT Name = <%=StrBoxName%> SIZE="1">
<OPTION SELECTED> <%=strDefault%> </OPTION>
<%do until rs.EOF%>
<OPTION> <%=rs(strFieldName)%> </OPTION>
<%rs.movenext
loop%>
</Select>

<%
'Close and clean up
'-------------------------------------------------------------------------
rs.close
set rs=nothing
End sub
%>
////////////////////
HTML section
////////////////////

strSQL = "SELECT orders.[orderid] FROM orders ORDER BY orders.[orderid] "
strFieldName = "orderid"
strDefault = "default value"
StrBoxName = "name"
strBoxTitle = "<B>title: </B>"
strConn = "DRIVER={Microsoft Access Driver (*.mdb)}; pwd=; DBQ="&
Server.MapPath("northwind.mdb")

Response.write ("Droplist box No.2" & "<BR>")
Call Droplist(strSQL,strFieldName,strDefault,StrBoxName,strBoxTitle,strConn)
 
D

dlbjr

With Response
.Write "<SELECT Name='"
.Write StrBoxName
.Write "' SIZE='1'>"
Do until rs.EOF
strItem = rs(strFieldName)
.Write "<OPTION VALUE='"
.Write strItem
.Write "'"
If CStr(strItem) = CStr(strDefault) Then
.Write " SELECTED"
End If
.Write ">"
.Write strItem
.Write "</OPTION>"
rs.movenext
loop
.Write "</SELECT>"
End With

'from dlbjr

'Unambit from meager knowledge of inane others,engender uncharted sagacity.
 
D

David C. Holley

Well first, very, very seldom is there a RIGHT method.
Second, its all about setting the value in the <option> tags as in...

response.write "<option value=""United"">United</option>"
-or perhaps-
response.write "<option value=""51"">Disney's Fort Wilderness Resort &
Campground</option>"

then its just a matter of getting the value either by javaScript or via
the request object if you submit to a new page.

Also, I personally have found that its not a good idea to specify which
option is SELECTED. Instead just write the particular option first in
the select. The browser should automatically display it first.

David H.
Hello
In this sample code there is a drop down box that is populated with values
from the database. What is the right method to capture this user selected
value and to use that value as the filter for another drop down box or even
as a value to be processed by by the page?

Thanks
Tom

<%
'************************************
'Subroutine for droplist box
'
'PURPOSE: code to be called for placement of droplist box in a form or page
'
'PARAMETERS:
'strSQL = Defines SQL statement
'strFieldName = Defines field name in database table
'strDefault = Defines default value in droplist
'StrBoxName = Defines name of droplistbox
'strBoxTitle = Defines the title shown next to droplist box on screen
'strConn = Defines the database connection string
'*************************************

Sub Droplist(strSQL,strFieldName,strDefault,StrBoxName,strBoxTitle,strConn)

'Set Cursor
'-------------------------------------------------------------------------
Const adOpenStatic=3

' create the recordset, open it, and move to first record
'-------------------------------------------------------------------------

Set rs = Server.CreateObject ("ADODB.Recordset")
rs.Open strSQL, strConn,adOpenStatic

rs.movefirst

'Ouput result to droplist box
'-------------------------------------------------------------------------

strBoxTitle%>
<SELECT Name = <%=StrBoxName%> SIZE="1">
<OPTION SELECTED> <%=strDefault%> </OPTION>
<%do until rs.EOF%>
<OPTION> <%=rs(strFieldName)%> </OPTION>
<%rs.movenext
loop%>
</Select>

<%
'Close and clean up
'-------------------------------------------------------------------------
rs.close
set rs=nothing
End sub
%>
////////////////////
HTML section
////////////////////

strSQL = "SELECT orders.[orderid] FROM orders ORDER BY orders.[orderid] "
strFieldName = "orderid"
strDefault = "default value"
StrBoxName = "name"
strBoxTitle = "<B>title: </B>"
strConn = "DRIVER={Microsoft Access Driver (*.mdb)}; pwd=; DBQ="&
Server.MapPath("northwind.mdb")

Response.write ("Droplist box No.2" & "<BR>")
Call Droplist(strSQL,strFieldName,strDefault,StrBoxName,strBoxTitle,strConn)
 
C

Captain Flack

David said:
Also, I personally have found that its not a good idea to specify which
option is SELECTED. Instead just write the particular option first in
the select. The browser should automatically display it first.

Just interested what your reasoning is for this? I think specifying
which is selected is better since most dropdowns are in an order that
makes sense, i.e. alphabetical list of countries, names, etc.
 
D

David Holley

As I recall, if you specify which option is SELECTED, if you're user is
using OPERA (www.opera.com), Opera will override the visitor's selection
in favor of the one that you've specified.

Additionally, I have a reservation form that submits back to itself to
so that the appropriate fields are displayed based on the reservation
type. When the form resubmits, I wanted the value displayed in the
reservation type select box. When I was developing this, I initially
wrote the SELECTED spec in the appropriate option. This worked fine,
unless I RESET the form and then it got a bit whacked out. The solution
which I went to was to write the selected option first.

David H
www.gatewayorlando.com

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top