Pick Lists

A 'Pick List' and a 'Select Box' are almost the same thing. Recall from the last lesson that you can get input from the viewer using a form. One of the form elements discussed was a select box. The coding looks like so:

<form action="#" method="post">Choose a state: <br /> <select name="State"> <option value="AL">Alabama</option> <option value="AK">Alaska</option> <option value="AZ">Arizona</option> <option value="AR">Arkansas</option> </select> </form>

which produces:

Choose a state:

This is a hard-coded solution to generating a select box. A pick list is identical, except the box is dynamically generated. Consider the following:

<form action="#" method="post">Choose a state:
<select name="State"> <?php $states_arr = array( "Alabama"=>"AL", "Alaska"=>"AK", "Arizona"=>"AZ", "Arkansas"=>"AR", "California"=>"CA", "Colorado"=>"CO", "Connecticut"=>"CT", "Delaware"=>"DE", "Washington DC"=>"DC", "Florida"=>"FL", "Georgia"=>"GA", "Guam"=>"GU", "Hawaii"=>"HI", "Idaho"=>"ID", "Illinois"=>"IL", "Indiana"=>"IN", "Iowa"=>"IA", "Kansas"=>"KS", "Kentucky"=>"KY", "Louisiana"=>"LA", "Maine"=>"ME", "Maryland"=>"MD", "Massachusetts"=>"MA", "Michigan"=>"MI", "Minnesota"=>"MN", "Mississippi"=>"MS", "Missouri"=>"MO", "Montana"=>"MT", "Nebraska"=>"NE", "Nevada"=>"NV", "New Hampshire"=>"NH", "New Jersey"=>"NJ", "New Mexico"=>"NM", "New York"=>"NY", "North Carolina"=>"NC", "North Dakota"=>"ND", "Ohio"=>"OH", "Oklahoma"=>"OK", "Oregon"=>"OR", "Pennsylvania"=>"PA", "Puerto Rico"=>"PR", "Rhode Island"=>"RI", "South Carolina"=>"SC", "South Dakota"=>"SD", "Tennessee"=>"TN", "Texas"=>"TX", "Utah"=>"UT", "Vermont"=>"VT", "Virgin Islands"=>"VI", "Virginia"=>"VA", "Washington"=>"WA", "West Virginia"=>"WV", "Wisconsin"=>"WI", "Wyoming"=>"WY" ); foreach ($states_arr As $key => $value) { echo "<option value='" .$value. "'>" .$key. "</option><br />\n"; } ?> </select> </form>

Everything appears as above, only the <option></option> tags were dynamically generated. The result appears below:

Choose a state:

Voila, a dynamically generated pick list.

You can also use data from a database to populate your <option></option> tags. I have a table that tracks information on each new visit to this site over the last 30 days. The table stores the date/time and language designation from the calling machine. I count the number of new visits on this table to produce the statistics shown in the footer below. Suppose I wanted to make a pick list of all the language designations that visited the site. The code would look like so:

<form action="#" method="post">Choose a language: <br /> <select name="lang"> <?php $query = "SELECT DISTINCT lang FROM stats ORDER BY lang"; $link = mysql_connect($host,$user,$pwd) or die("Could not connect to Host."); mysql_select_db($db) or die("Could not select database."); $result = mysql_query($query) or die("Could not run query."); while ($answer = mysql_fetch_array($result)) { extract($answer); echo "<option>" .$lang. "</option><br />\n"; } ?> </select> </form>

which produces (can you find your own?):

Choose a language:

Note again that the <option></option> tags are dynamically generated.

Forms   < <  PREVIOUS   Table of Contents NEXT  > >   Handlers

Developed with HTML-Kit
Sandersongs Web Tutorials
Contact the Webmasterwith comments.
©2018, by Bill Sanders, all rights reserved.
This domain had 3,468 different visits in the last 30 days.
447,881 hits on this domain since 24 Nov 2006.
This page was last modified on our server on 4 Jun 2008
and last refreshed on our server at 8:35 am, MST
This file took 0.01272 seconds to process.