Note
I needed to translate bunch of things from my native language, Polish, and had no chance to test the updated version, so if something does not work, please let me know.
The function
<?php
/**
* dropMenu Function
*
* This function generates an HTML dropdown menu with various customization options.
* It is designed for ease of use and flexibility in creating dropdown menus.
*
* @param string $f_name - The name attribute for the dropdown.
* @param mixed $f_value - The selected value for the dropdown.
* @param array $f_array - An associative array of key-value pairs for dropdown options.
* @param string $f_default - Default text for the initial option (optional).
* @param string $f_css - Additional CSS classes for styling (optional).
* @param string $f_tabindex - The tabindex attribute for keyboard navigation (optional).
* @param string $f_js - Additional JavaScript attributes (optional).
*
* @return void Outputs the HTML for the dropdown menu.
*
* You are free to use this function without limits. If you include the name or both
* the name and URL in your code, attribution to the original author is appreciated.
*
* Usage example:
* dropMenu('us_state', '', $states_arr, 'Select State', '', '', '');
*/
function dropMenu($f_name, $f_value, $f_array, $f_default='', $f_css='', $f_tabindex='', $f_js='')
{
echo '<select name="' . $f_name . '" id="' . $f_name . '"';
if ($f_css != '') echo ' class="' . $f_css . '"';
if ($f_js != '') echo ' ' . $f_js;
if ($f_tabindex != '') echo ' tabindex="' . $f_tabindex . '"';
echo ">";
if($f_default != '') echo "n<option value="">" . $f_default . "</option>n";
foreach ($f_array as $key => $val) {
$output = '<option value="' . $key . '"';
if ($key == $f_value) {
$output .= ' selected';
}
$output .= '>' . $val . '</option>\n';
echo $output;
}
echo"</select>";
/* *** dropMenu function ends *** */
}
So, to the point. With this function, you will need an array defined somewhere. Maybe in an included file or somewhere within the page itself. Let's use few of US States' as an example.
<?php
/* *** create an array with the States *** */
$states_arr = array (
'CA' => 'California',
'NV' => 'Nevada',
'TX' => 'Texas'
);
/* *** initialize drop-down function *** */
dropMenu('us_state', '', $states_arr, 'Select State', '', '', '');
/* *** or, since you don't need those extra parameters for now, you can shorten the init code: *** */
dropMenu('us_state', '', $states_arr, 'Select State');
How does it look? Here we go:
HTML output:
<select name="us_state" id="us_state">
<option value="">Select State</option>
<option value="CA">California</option>
<option value="NV">Nevada</option>
<option value="TX">Texas</option>
</select>
So this the base, something you call when an User fills the form for the first time.
PRE-SELECTING output.
Now, most likely you will also need the same User to update his/her information. While at it: sooner or later this will come in handy to already print out the initial selection with the variable taken out from the array.
The second option in the function will help you out here.
All you need to do is to define the initial state.
<?php
dropMenu('us_state', 'CA', $states_arr, 'Select State', '', '', '');
HTML output:
<select name="us_state" id="us_state">
<option value="">Select State</option>
<option value="CA" selected>California</option>
<option value="NV">Nevada</option>
<option value="TX">Texas</option>
</select>
Or Nevada:
<?php
dropMenu('us_state', 'NV', $states_arr, 'Select Nevada State', '', '', '');
HTML output:
<select name="us_state" id="us_state">
<option value="">Select Nevada State</option>
<option value="CA">California</option>
<option value="NV" selected>Nevada</option>
<option value="TX">Texas</option>
</select>
The second value in the funtion should take its value dynamically, meaning a simple IF condition will do the job just fine:
IF
value is defined
$my_value = defined value
ELSE
$my_value = '';
Final version
<?php
dropMenu('us_state', $my_value, $states_arr, 'Select State', '', '', '');
...and you're all set.
Additional parameters
By default the additional parameters are empty, so all you need to do is to fill the proper variable with your own input.
1. Add CSS to the dropdown
<?php
dropMenu('us_state', 'CA', $states_arr, 'Select State', 'free_db_class', '', '');
2. Tabindex.
If you want your form's elements to be focused in particular order, you can add a value to the next-to-last parameter of the dropdown function.
<?php
dropMenu('us_state', 'TX', $states_arr, '-Select State-', '', '3', '');
3. Inline JavaScript (jump menu)
<?php
dropMenu('us_states', '', $states_arr_up, ' - Drop with Jump option - ', '', '', 'onchange="document.location.href=this.value"');
Add all three at once
<?php
dropMenu('us_states', '', $states_arr_up, 'Drop with all parameters enabled', 'free_db_class', '2', 'onchange="document.location.href=this.value"');