Dynamically selecting single-option dropdown box entries:

 <?php
// We have a select box form variable named 'menu'
// We want to have a dynamic item in that box selected
// Prepare the selection variable
unset($sel_menu);
unset(
$sel_menu2);
$sel_menu[$_POST['menu']] = "SELECTED";

// Print "SELECTED" in the proper option
// The other options will have blank values
echo "
   <select name=menu>
      <option value=''>Default</option>
      <option {$sel_menu['val1']} value='val1'>Pork</option>
      <option {$sel_menu['val2']} value='val2'>Yes</option>
      <option {$sel_menu['val3']} value='val3'>Philanthropy</option>
      <option {$sel_menu['val4']} value='val4'>Amiga</option>
   </select>
   "
;
 
?>

Example:

 


Dynamically selecting multiple-option dropdown box entries:

 <?php
// We have a select box form variable named 'menu'
// We want to have a dynamic item in that box selected
// Prepare the selection variable
if(is_array($_POST['menu2']) //check to ensure that an array has been sent
{
   foreach(
$_POST['menu2'] as $menuitem)    //loop through each value sent in the array
      
$sel_menu[$menuitem] = "SELECTED";  //mark the indicated values as "selected"
}

// Print "SELECTED" in the proper option
// The other options will have blank values
echo "
   <select name=menu2[] multiple>
      <option {$sel_menu['val1']} value='val1'>Pork</option>
      <option {$sel_menu['val2']} value='val2'>Yes</option>
      <option {$sel_menu['val3']} value='val3'>Philanthropy</option>
      <option {$sel_menu['val4']} value='val4'>Amiga</option>
   </select>
   "
;
 
?>

Example: