Home > Programming, php > php: Dynamically fill a select box

php: Dynamically fill a select box

December 30th, 2005 ^Lestat Leave a comment Go to comments

One way to fill a select box with array elements.

<html>
<head>
<title>Select Box</title>
</head>
<body>

< ?php
$myArray = array('cat' , 'dog' , 'mouse' , 'cow');
?>

<select name="myselect">
<option value=""></option> // Sets blank entry *optional*
< ?php
foreach($myArray as $value){ // Loop through each element
  print("<option value=\"$value\">$value<option>");
}
?>
</option></select>

</body>
</html>

php, select, drop down

Categories: Programming, php Tags:
  1. May 20th, 2008 at 14:16 | #1

    Thanks, I was having trouble with this.

  1. No trackbacks yet.