N
N
Nazar Jeveling2014-10-07 17:43:06
Yii
Nazar Jeveling, 2014-10-07 17:43:06

How to make dynamic select using PHP Yii?

There are two selects, when you select one value in the first, the values ​​in the second should change, (example: country-> region)
After reading the Yii Framework documentation, this is what I did:
page

// первый select
<?= CHtml::dropDownList('offerDetailType', '', array(
                        'CONVERTIBLE_NOTE' => 'Convertible Note',
                        'DEBT' => 'Debt',
                        'EQUITY' => 'Equity'
                    ), array(
                        'ajax' => array(
                            'type' => 'POST',
                            'url' => CController::createUrl('startupController/instrumentType'),
                            'update' => '#instrumentType'
                        ),
                        'empty' => 'Select Type',
                        'class' => 'form-control'
                    )); ?>
// второй select
<?= CHtml::dropDownList('instrumentType', '', array(), array('class' => 'form-control')); ?>

here is what is in the controller
public function actionInstrumentType() {
        switch ($_POST['offerDetailType']) {
            case 'CONVERTIBLE_NOTE':
                $data = array('CONVERTIBLE_NOTE' => 'Convertible Note');
                break;
            case 'DEBT':
                $data = array('NOTE_DEBENTURE' => 'Debenture');
                break;
            case 'EQUITY':
                $data = array(
                    'SHARE_COMMON' => 'Common',
                    'SHARE_PREFERRED' => 'Preferred'
                );
                break;
        }
        $data = CHtml::listData($data,'id','name');
        foreach ($data as $value=>$name) {
            echo CHtml::tag('option', array('value'=>$value),CHtml::encode($name), true);
        }
    }

After selecting anything in the first select, nothing happens, but the second one is empty, what am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Cherednichenko, 2014-10-07
@likeapimp

It's better to use jquery here

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question