Populating A Drop-Down List In PHP

 

This script will show you how to populate a drop-down list in php.

 

Example

 

 

Script Source

 

<fieldset><label>Selection List:</label>
<?php
echo "<select name=\"opt\" id=\"opt\">\n";
$optArr = array();
$optArr[] = "Selection one";
$optArr[] = "Selection two";
$optArr[] = "Selection three";
$optArr[] = "Selection four";
$optArr[] = "Selection five";
$optArr[] = "Selection six";
foreach($optArr as $v){
if ($opt == $v) {
echo "<option value=\"$v\" selected=\"selected\">$v</option>\n";
} else {
echo "<option value=\"$v\">$v</option>\n";
}
}
echo "</select>";
?>
</fieldset>

 

Please let me know if this script was helpful to you.

[rating]