Dependent Select Fields

Yesterday I was looking for a way to make a select field dependent on the selected values of another select field. Below is the form code that I am using, and I hope it helps others looking to do the same thing. NOTE: this is not for the join form, but for custom forms that you create using the format in the example in inc/classes/BxDolForm.php.

 

function getBlockCode_AddAddictions()
    {
      
        $myJavaScript = <<<CODE
<script type="text/javascript">

 function setOptions(chosen) {
 var selbox = document.form_addictions.AddictionName;
 
 selbox.options.length = 0;
 if (chosen == "") {
   selbox.options[selbox.options.length] =
new Option('Please select one of the options above first','Please select one of the options above first'); 
 }
 if (chosen == "Nicotine") {
   selbox.options[selbox.options.length] =
new Option('Cigarettes','Cigarettes');
   selbox.options[selbox.options.length] =
new Option('Chewing Tobacco','Chewing Tobacco');
   selbox.options[selbox.options.length] =
new Option('Pipe','Pipe');
   selbox.options[selbox.options.length] =
new Option('Other','Other');
 }
 if (chosen == "Alcohol") {
   selbox.options[selbox.options.length] =
new Option('Beer','Beer');
   selbox.options[selbox.options.length] =
new Option('Liquor','Liquor');
   selbox.options[selbox.options.length] =
new Option('Wine','Wine');
 }
 if (chosen == "Other Addiction") {
   selbox.options[selbox.options.length] =
new Option('Work','Work');
   selbox.options[selbox.options.length] =
new Option('Stimulants','Stimulants');
   selbox.options[selbox.options.length] =
new Option('Solvents','Solvents');
   selbox.options[selbox.options.length] =
new Option('Sex','Sex');
   selbox.options[selbox.options.length] =
new Option('Relationships','Relationships');
   selbox.options[selbox.options.length] =
new Option('Pornography','Pornography');
   selbox.options[selbox.options.length] =
new Option('Benzodiazepines','Benzodiazepines');
   selbox.options[selbox.options.length] =
new Option('Caffeine','Caffeine');
   selbox.options[selbox.options.length] =
new Option('Eating Disorders','Eating Disorders');
   selbox.options[selbox.options.length] =
new Option('Exercise','Exercise');
   selbox.options[selbox.options.length] =
new Option('Food','Food');
   selbox.options[selbox.options.length] =
new Option('Gambling','Gambling');
   selbox.options[selbox.options.length] =
new Option('Hallucinogens','Hallucinogens');
   selbox.options[selbox.options.length] =
new Option('Money','Money');
   selbox.options[selbox.options.length] =
new Option('Internet','Internet');
   selbox.options[selbox.options.length] =
new Option('Opiates','Opiates');
   selbox.options[selbox.options.length] =
new Option('Others','Others');
 }
 }
   
// -->
</script>
CODE;


        $aForm = array(
            'form_attrs' => array(
                'name'     => 'form_addictions',
                'method'   => 'post',
            ),

            'params' => array (
                'db' => array(
                    'table' => 'cf_addictions',
                    'key' => 'addictions_id',
                    'uri' => '', // uri field name
                    'uri_title' => '',
                    'submit_name' => 'submit_form',
                ),
                'csrf' => array(
                      'disable' => true,
                )
              ),

            'inputs' => array(
           
                'AddictionType' => array(
                    'type' => 'select',
                    'name' => 'AddictionType', // the same as key and database field name
                    'caption' => _t('_cf_complete_addictions_type'),
                    'required' => true,
                    'values' => array (
                        '' => '',
                        'Nicotine' => 'Nicotine',
                        'Alcohol' => 'Alcohol',
                        'Other Addiction' => 'Other Addiction',
                    ),
                    'attrs' => array(
                    'id' => 'AddictionType',
                    'onchange' => 'setOptions(document.form_addictions.AddictionType.options[ document.form_addictions.AddictionType.selectedIndex].value);',
                    ),
                    // checker params
                    'checker' => array (
                        'func' => 'length',
                        'params' => array(1,100),
                        'error' => _t('_cf_complete_addictions_type_err'),
                    ),
                    // database params
                    'db' => array (
                        'pass' => 'Xss', 
                    ),
                ),

                'AddictionName' => array(
                    'type' => 'select',
                    'name' => 'AddictionName',
                    'caption' => _t('_cf_complete_addictions_name'),
                    'required' => true,
                    // checker params
                    'checker' => array (
                        'func' => 'length',
                        'params' => array(1,100),
                        'error' => _t('_cf_complete_addictions_name_err'),
                    ),
                    'attrs' => array(
                    'id' => 'AddictionName',
                    ),
                    // database params
                    'db' => array (
                        'pass' => 'Xss',  // do XSS clear before getting this value, see BxDolFormCheckerHelper class for all pass* functions
                    ),
                ),
                'Submit' => array (
                    'type' => 'submit',
                    'name' => 'submit_form',
                    'value' => _t('_Submit'),
                    'colspan' => true,
                ),
                           
            ),
         );
         
        $oForm = new BxTemplFormView ($aForm);
        $oForm->initChecker();

        if ($oForm->isSubmittedAndValid ()) {

            // add additional vars to database, in this case creation date field is added
            $aValsAdd = array (
                'date' => time(),
            );

            if ($oForm->insert ($aValsAdd))
            $sStatusText = '_Save addictions successful';

        }
                if($sStatusText)
            $sStatusText = MsgBox(_t($sStatusText), 3);
        return array($sStatusText . $myJavaScript . $oForm->getCode(), array(), array(), false);
    }

 

(i removed a few fields from the form in my example above to reduce the amount of space used.

When a user selects an "Addiction Type", the corresponding values are available in the "Addiction Name" field.

Hope this helps. Also, thanks to Deano. He inadvertently helped with this by helping me with another problem.

caredesign.net
Quote · 9 Apr 2014
 
 
Below is the legacy version of the Boonex site, maintained for Dolphin.Pro 7.x support.
The new Dolphin solution is powered by UNA Community Management System.