Alexandar Tzanov's Personal Blog

Function: popupMutableMenu

The popupMutableMenu PHP function, is similar to the popupMenu function, with the exception that it will allow the user to enter their own value. The popup menu will be generated from a text file. If the file cannot be read or found the function will return a regular text field. The purpose of this function is to generate a menu with a long list of options. Another benefit of this function is to reuse it in different views, but to only have to maintain the list of item in one place – the text file.

The menu is generated with an additional option “Other”. If a user select the Other option a free form text field will be displayed beneath the popup menu. The option will be added to the menu as the user types.

When the menu is generated a custom value option will be added to the list if a value string is provided but the string is not found in the menu list file. This is useful in situation when the user is returned to the page with a pre-populated form data, e.g. during validation, or editing.

Source Code

/* popupMutableMenu
 * Developer: Alexandar Tzanov
 * Revised: 2013-08-29
 * Version: 1.0

function popupMutableMenu($fileName = '', $relevantPath = '', $value = '', $name = '', $id = 'popupMenu', $size = 40)
    // Get working directory path.
    $workDir = getcwd();
    $dataFile = "$workDir/$relevantPath/$fileName";
    // Make sure the file exist, then read it.
    if (!empty($fileName) && file_exists($dataFile))
        $fileData = file($dataFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
        // JavaScript controlls for the Other text field and menu option.
        $otherControlls = '';
        // Popup menu
        $popupMenu = $otherControlls;
        $popupMenu .= "\n";
        $popupMenu .= "
\n"; // Return popup menu code. return $popupMenu; } else { // Return an input field with a value, if any. return ""; } }


string popupMenu( string $fileName[,string $relevantPath[, string $value[, string $name = popupMenu[, string $id = popupMenu[, int $size = 40]]]]])


Name Definition Default Value
$fileName The name of the text file with the items. One option item per line. N/A
$relevantPath The local path to the file starting from the product’s work directory. The work directory is automatically detected. N/A
$value The value to match agains to preselect an option. N/A
$name The name attribute of the select/input HTML element. popupMenu
$id The id attribute of the select/input HTML element. popupMenu
$size The size of the input text field. 40


The popupMenu function will return one of two possible form field.

  • A popup menu, i.e. a select with options, if a readable text file with a name and at location specified by the developer is found.
  • A text field, if the file specified by the developer is either not readable or cannot be found.


// Example 1 - Generate the options menu.
echo popupMenu('list-of-items.txt', 'bin');

// Example 2 - Generate the options menu with a preselected option.
print popupMenu('itemslist.txt', '', 'Apple');

Revision History

Date Version Changes
2013-08-29 01.00.00 Initial release.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.