Friday, 10 August 2018

Generate A Select Box For A HTML Form

Here is a function to generate a select box for a HTML form. The three parameters are:
  1. $name : The name of the select, this will appear in the "name" attribute.
  2. $options : An associative array containing all of the options.
  3. $default : The option that will be selected as default.
  1. function generateSelect($name,$options,$default=''){
  2. $html = '';
  3. foreach ($options as $value => $label) {
  4. $html .= '' . $label . '';
  5. }
  6. $html .= '';
  7. return $html;
  8. }
You can call the function like this:
echo generateSelect('selectPreference',array('yes'=>'yes','no'=>'no'),'yes');
Which produces the following HTML as output:
yesno

0 comments:

Post a Comment