and like us on FB

Populating a HTML dropdown box given the values of an array, or populating a dropdown box from values in a database, is a very easy and useful tool that demonstrates the simplistic power of PHP. Below are a few examples. This was written in 2005. Since then, of course, the MySQL functions are largely unsupported… but we’ll keep them for the sake of the examples.

The end result of the below code will give you something that looks like this:


The following code will populate a dropdown box with an option name.
Example: <option value="Sydney">Sydney</option>

The following code will populate a dropdown box with an option value and name.
Example: <option value="s">Sydney</option>

More often than not, you’ll want to populate a dropdown box based on the values retrieved from a database.

It’s almost always best to build these features into a function and return the result.

Shortt URL for this post: http://shor.tt/Dx4