|
For this example, I will use a list of cities for the client to choose
from.
I won't get into the connection string. If you need to see how to
connect to the database, several different examples are explained here.
You can also find out how to enter it into a database here.
Now that you are connected let's get the dropdown box populated.
This example will make a few assumptions. I will outline them now.
- The page that will process the form is
named next_page.asp.
- The database has a table named myTable
with a field named city.
- The connection string is named Conn.
assumption 1
<form method="POST" action="next_page.asp">
<p><select size="1" name="D1">
<option selected>City</option>
<%
'assumption 2
SQL = "Select city FROM myTable ORDER BY city ASC"
'assumption 3
SET RS = Conn.Execute(SQL)
While NOT RS.EOF
%>
<option value="<%=RS("city")%>"><%=RS("city")%></option>
<%
RS.MoveNext
WEND
Set RS = NOTHING
Conn.Close
%>
</select><input type="submit" value="Submit" name="B1"></p>
</form>
Now let's look at the code to get an
understanding of what it is saying.
SQL = "Select city FROM myTable
ORDER BY city ASC"
This says "open the table named myTable and look at if from the
field named city in alphabetical order.
SET RS = Conn.Execute(SQL)
Now it is creating a recordset based on what the SQL statement said to
do.
While NOT RS.EOF
In plane English this says "Since we aren't at the end of the
field named city in the table, write this to the page:"
<option value="<%=RS("city")%>"><%=RS("city")%></option>
<%=RS("city")%>
This will write the value of one record (alphabetically mind you).
RS.MoveNext
Since we still aren't to the end of the field write the next record.
WEND
Since we ran out of things to write, STOP what you're doing.
Set RS = NOTHING
Kill the recordset to free up the server resources you were using.
ALWAYS DO THIS!
Conn.Close
Close the database connection.
|