Live Select - Drop Down Form Fields

How to generate select and dynamic select fields

When using live inputs we need to have the following option types on our section: L ,

Or we can manually adding them by setting the option types live, form and update to true and including the following JS library:

<!-- Live edit functions -->
<script type="text/javascript" src="/kas/js/live_edit.js?v=1606744636"></script>

<?php
   $options['add'] = false;  //make sure add is off in case there is an add form above the live inputs
   $options['live'] = true;
   $options['form'] = true;
   $options['update'] = true;
   $options['live'] = true;
?>

 

Live inputs generate an encrypted data containing the table name, column name and id value of the record to be changed. Most of the times we already have those variables in use:

 

$dbtable = 'TABLE_NAME';
$idfield = 'COLUMN_NAME';
// $idvalue generated automatically when the URL has an ID 

 

if we don't have those variables set, we can pass them to the live input with the 'encdata' value.

 

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'=> 'live',
  'inputType'=> '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'],
     ...
  ],   
  'encdata'=>[  //optional unless $dbtable, $idfield and $idvalue are not declared, 
      'TYPE'    =>'LIVE',
      'DBTABLE' =>$dbtable,
      'IDFIELD' =>$idfield,
      'IDVALUE' =>$idvalue
   ]
]);

 

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

 

addField([
  'type'=> 'live',
  'inputType'=> '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
  ],   
  'encdata'=>[  //optional unless $dbtable, $idfield and $idvalue are not declared, 
      'TYPE'    =>'LIVE',
      'DBTABLE' =>$dbtable,
      'IDFIELD' =>$idfield,
      'IDVALUE' =>$idvalue
   ]
]);


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
  ],   
  'encdata'=>[  //optional unless $dbtable, $idfield and $idvalue are not declared, 
      'TYPE'    =>'LIVE',
      'DBTABLE' =>$dbtable,
      'IDFIELD' =>$idfield,
      'IDVALUE' =>$idvalue
   ]
]);

 

(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