Select - Drop Down Form Fields

How to generate select and dynamic select fields

Using the addField() function we can generate selects by simply parsing specific values in an array or specific settings to generate it from database values.

The minimun parameters to pass are:

addField([
  'type'=> 'select',
  'label'=> 'Label Title',
  'name'=> 'colName',  //optional
  'value'=> 'selected value', //optional, without one a "Select an option" option appears
  'options'=> [       //required, unless optionsdb is used (see next)
     0 =>['name' => 'OP1','value'=> 'VAL1'],
     1 =>['name' => 'OP2','value'=> 'VAL2'],
     2 =>['name' => 'OP3','value'=> 'VAL3'],
     ...
  ]
]);

 

If we want to get the options data from a database our array needs the following keys:

 

addField([
  'type'=> 'select',
  'label'=> 'Label Title',
  'name'=> 'colName',  //optional
  'value'=> 'selected value', //optional, without one a "Select an option" option appears
  'optionsdb'=> [       //required if options is not used
     'table' => 'TABLE NAME', //required
     'op_name' => 'COL_NAME', //required
     'op_val' => 'COL_NAME',  //required
     'where' => 'WHERE STATEMENT', //optional
     'order' => 'ORDER STATEMENT'  //optional
  ]
]);


For example:

 
'optionsdb'=> [
   "table" => "CLIENT_TYPE",
   "op_name" => "type_$mainLang",
   "op_val" => "CTID",
   "where" => "type_$mainLang != '' ",
   "order" => "order by type_$mainLang"
];

 

All the options

 

 

addField([
  'type'=> 'select',
  'label'=> 'Label Title',
  'name'=> 'colName',  //optional
  'id' => 'selID', //optional, name is used as id if id is null
  'value'=> 'selected value', //optional, without one a "Select an option" option appears
  'attributes' => 'required', //optional
  'style' => 'border:1px solid red', //optional
  'class' => '', //optional
  'options'=> array(), //optional, required if optionsdb is not used
  'optionsdb'=> [       //required if options is not used
     'table' => 'TABLE NAME', //required
     'op_name' => 'COL_NAME', //required
     'op_val' => 'COL_NAME',  //required
     'where' => 'WHERE STATEMENT', //optional
     'order' => 'ORDER STATEMENT'  //optional
  ]
]);

 

(The old way) You can also parse a CSV string with : as the delimiter between names and values. 

$selValues = "value:name,value:name,value:name"
 
example:
$selValues = 'VI:Visa,MC:Mastercard,AMEX:American Express';
addField('select','CC Provider','CC_PROVIDER:',$selValues);
 
Get a Quote

Get a Quote