Tutorials & Script Homepage
Design Theory 22 to 28
Web Page Mock Ups
Populating a Drop Down from a Database
Top Ten Web Design Tips
Design Usability and Graphics Compression
Host With Us: HostingHelper.co.uk From Only $5.00p/m
Spotted an Error ? Let us Know
100's more FREE templates at FreeWebTemplates
Looking for a new Web Host ?
Free templates from TemplatesLand
Professional High-End Templates
TemplateBox.com more FREE templates
Free Flash Templates and Intros
Please Click Here, and Help Keep This Site FREE to Use
Please click on our sponsors advert to the right if you download or use anything from this page.
Thank you for your help.
This article has been kindly written for WebDesignHelper.co.uk by AndyRed, Insight2Design Ltd, http://www.insight2design.com/

Forms - Populating a Drop Down Menu with Info. from a Database

Surviving the basics
A drop down menu is the most popular way to cram a lot of links into a small space. A drop down menu (also called a "drop down") is a list of web pages. The user selects one of the options and presses, the choice that has been selected will then stay selected permanently. For example, this drop down gives you three options:

The code for the basic drop down list is like this

<SELECT NAME="search" style="width:200;" action ="http://www.insight2design.com/index.asp">
<OPTION VALUE="alatvista">Altavista
<OPTION VALUE="google">Google
<OPTION VALUE="lycos">Lycos

Using Active Server Page's (ASP) to our advantage
Drop Downs are tremendously useful and they are commonly used for forms throughout the WWW. We can use our new found programming language to our advantage to develop a easier way to manage our drop down lists, and that method involves building a database

Firstly what we need to do is build our database to hold our information for the drop down list. In this example we will build a drop down which will contain a list of countries around the world.

Our database table would look like.

Unid Varchar_country Show
Int - Identity Varchar(50) Int(4)

Entering the information
Once the table has been built, we need to enter our information.

We enter into the second column each different country we need to enter and for each country we would like to appear on the page we enter a 1 into the show column. For every country we do not want to appear we enter a 0 in the show column. Something like this below.

Unid Varchar_country Show
1 United Kingdom 1
2 United States Of America 1
3 France 0

Developing our Active Server Page
Once the table has been built, start to build our ASP page. The below code is the top of the HTML code.

<form method="post" name="countryform" action ="http://www.insight2design.com/index.asp">
<SELECT name=country>
<option value=PS>Please Select</OPTION>

Now we start to write out the ASP code.

sTestSQL = "SELECT * FROM tbldropdown_country where show = 1 order by varchar_country asc"
Set connTest = Server.CreateObject("ADODB.Connection")
connTest.Open "dsn=mynewdsn;Uid=username;Pwd=password;"
Set rsTest = connTest.Execute(sTestSQL)

The variable sTestSQL set in the second line of the ASP code holds the SQL statement which will pull all the information out of the table we have previously created. The third line creates the server object for the database, whilst the fourth line specifies the link to our database using DSN. The last line of the code executes the SQL statement contained in sTestSQL on our table.

Do While Not rsTest.EOF
            Response.Write "<Option value='" & rsTest("varchar_country") & "'>" & _
            rsTest("varchar_country") & "</option">

We know loop through all the values displaying the shown fields from our table. The ASP code not only displays the drop down information but also the drop down value. You can check this on the finished article by viewing the source and seeing if the two values are in the Option tag.

Response.write "</select>"
Set rsTest = Nothing
Set connTest = Nothing

The last lines of code finish off the drop down box. The Response.write line of the code closes the select tag, the second and third close the record set and four and five closes the database connection

The advantages of adding a database driven drop down list are as follows:

  • Easy to maintain
  • Saves replicating data, that is stored in a database, onto a HTML page
  • More accurate
Insight2Design Ltd

Web Hosting  |  Membership website templates  |  Reliable UK Web Hosting  |  dresses  |  Art Web Templates  |  Cheap Flight Deals  |  UK Web Hosting Company  |  Coursework Writing  |  Reseller Hosting  |  coches de ocasion  |  Cheap UK Web Hosting  |   cell phones  |  Web Design  |  Low Cost Domain Names  |  Website templates  |  master a distancia  |  Austin Web Design  |  Flash Templates  |  Factory  |   cell phones  |  Canadian Web Hosting  |  Flash Intros  |  Web Templates  |  cartuchos  |  Corporate Logo Design Service  |  Website Templates  |  Bespoke Software Development  |  Domain Name Registration  |  Free Website Templates  |   Dedicated Server  |  UK Web Hosting  |  Dating Software - aeDating 4.1  |  Website Templates  |  Power Saver  |  taobao agent  |  Flash templates  |  cctv cameras  |  Lolita Dresse  |  china wholesale  |  Canadian Web Hosting  |   Cheap Web Hosting Deal  |  Linux Hosting  |  Web Design uk  |  Web Hosting  |  Business web directory  |  Electronics  |  Free domain names  |  Best UK web Hosting  |  Shower Enclosures UK  |  

  Top-end templates
  Flash Templates
  Web Hosting UK
  Web Design Tips
  Web Hosting Reviews
  Web hosting reviews
  Website Design
  Top web hosting
  Stock Photography
  Reseller Hosting
  Company Logos
  Website Templates
  Website Templates
  Web Hosting UK
  Logo Design Software
  Webmaster Resoucres
  Web Templates Factory
  Free Web Templates
  Free Templates Catalog
  Web Templates
  Reseller Web Hosting
  Stock Photography
  Host Review
  Web Hosting Host Color
  Dedicated Servers
  Build a free website
  web Hosting
  Business Logo Design
  Templates Favpage
  More Featured Sites