Dependent Dropdown

I have this two dropdownlist:

<?php echo $form->dropDownList($account, ‘Status1’, array(‘1’ => ‘Active’, ‘2’ => ‘InActive’), array(‘prompt’ => ‘’));

        ?&gt;

<?php echo $form->dropDownList($account, ‘Status2’, array(‘1’ => ‘Locked by Attempt’, ‘2’ => ‘Expired’), array(‘prompt’ => ‘’));

        ?&gt;

When I choose Active form dropdown status1 dropdown status2 will be disabled and when I chose InActive dropdown status2 will be enabled and can show data 1 and 2,

How can I manage to do this in yii?

Can you please help me with this problem, Im having a hard time on using jquery on yii :(

Is there a way on creating dependent dropdown using CFormModel, the wikis about this lessons are using CActiveRecord

You can add this code, for attribute "onchange"




<?php echo $form->dropDownList($account, 

                       'Status1', 

                        array('1' => 'Active', '2' => 'InActive'),

                        array(

                          'prompt' => '',

                          'onchange'=>'function(){ if($("#Status1").val()==1){$("#Status2").attr("disabled",    "disabled"); } else {$("#Status2").removeAttr("disabled");}}'

                       )

                     );

?>




oh so my view file will look like this?

<?php echo $form->dropDownList($account,

                   'Status1', 


                    array('1' =&gt; 'Active', '2' =&gt; 'InActive'),


                    array(


                      'prompt' =&gt; '',


                      'onchange'=&gt;'function(){ if(&#036;(&quot;#Status1&quot;).val()==1){&#036;(&quot;#Status2&quot;).attr(&quot;disabled&quot;,    &quot;disabled&quot;); } else {&#036;(&quot;#Status2&quot;).removeAttr(&quot;disabled&quot;);}}'


                   )


                 );

?>

<?php echo $form->dropDownList($account, ‘Status2’, array(‘1’ => ‘Locked by Attempt’, ‘2’ => ‘Expired’), array(‘prompt’ => ‘’));

?>

But still Status2 is still enabled when I chose 1

Remove from onchange "function(){}"




<?php echo $form->dropDownList($account, 

                       'Status1', 

                        array('1' => 'Active', '2' => 'InActive'),

                        array(

                          'prompt' => '',

                          'onchange'=>' if($("#Status1").val()==1){$("#Status2").attr("disabled",    "disabled"); } else {$("#Status2").removeAttr("disabled");}'

                       )

                     );

?>



Still not working, do you have any examples using dependent dropdowns? I checked on the wiki using dependent dropdowns but It usus CActiveRecord and Im using CFormModel.

That should work!




<?php echo $form->dropDownList($account, 

                       'Status1', 

                        array('1' => 'Active', '2' => 'InActive'),

                        array(

                          'prompt' => '',

                          'onchange'=>' if($("#ModelName_Status1").val()==1){$("#ModelName_Status2").attr("disabled",    "disabled"); } else {$("#ModelName_Status2").removeAttr("disabled");}'

                       )

                     );

?>



Assuming you are using model “User” #ModelName_Status1 is #User_Status1; it’s the id for form input element ;)

but when I chose Inactive in my status1 dropdown I can still select data from status2, so it means its still enabled, I want to do is when I selectInactive in my status1 dropdown, my second dropdown will be automatically disabled

Would you paste your codes here so that we can help you better ?


<?php $this->pageTitle = Yii::app()->name . ' - Account Creation'; ?>


            <?php

$this->menu=array(

    array('label'=>'Create Account', 'url'=>array('create')),

	array('label'=>'Update Account', 'url'=>array('view')),

    array('label'=>'Search Account', 'url'=>array('search')),

    array('label'=>'Lock Account', 'url'=>array('lock')),

    array('label'=>'Unlock Account', 'url'=>array('unlock')),

    array('label'=>'Audit Trail', 'url'=>array('audittrail')),

);


            ?>

<?php if (Yii::app()->user->hasFlash('account')): ?>

    <div class="flash-success">

        <?php echo Yii::app()->user->getFlash('account'); ?>

    </div>


<?php else: ?>

                <h1>Register New Account</h1>

    <div class="form">

    


            <?php

            $form = $this->beginWidget('CActiveForm', array(

                'id' => 'AccountForm',

                'enableClientValidation' => true,

                'clientOptions' => array(

                    'validateOnSubmit' => true,

                ),

                    ));            

   ?>

            

            <?php

            if (isset($flash))

                echo($flash);

            $AccountType = new RefAccountType();

            ?>

            

            

            <?php echo $form->errorSummary($account); ?>




            <div class="row">

                <?php echo $form->labelEx($account, 'username'); ?>

                <?php echo $form->textField($account, 'username',array('maxlength' => 20)); ?>

                <?php echo $form->error($account, 'username'); ?>

            </div>


            <div class="row">

                <?php echo $form->labelEx($account, 'AccountTypeName'); ?>


                <?php echo $form->dropDownList($account, 'AccountTypeName', $AccountType->getAccounttype(), array('prompt' => '')); ?>

                <?php echo $form->error($account, 'AccountTypeName'); ?>

            </div>


            <div class="row">

                <?php echo $form->labelEx($account, 'AccountGroup'); ?>

                <?php echo $form->dropDownList($account, 'AccountGroup', $AccountType->getAccountgroup(), array('prompt' => '')); ?>

                <?php echo $form->error($account, 'AccountGroup'); ?>

            </div>


<?php echo $form->dropDownList($account, 

                       'Status1', 

                        array('1' => 'Active', '2' => 'InActive'),

                        array(

                          'prompt' => '',

                          'onchange'=>' if($("#Account_Status1").val()==1){$("#Account_Status2").attr("disabled",    "disabled"); } else {$("#Account_Status2").removeAttr("disabled");}'

                       )

                     );

?>

        

        <?php echo $form->dropDownList($account, 'Status2', array('1' => 'Locked by Attempt', '2' => 'Expired'), array('prompt' => ''));?>

        

            <div class="row">

                <?php echo $form->labelEx($account, 'name'); ?>

                <?php echo $form->textField($account, 'name', array('size' => 38, 'maxlength' => 50)); ?>

                <?php echo $form->error($account, 'name'); ?>

            </div>


            <div class="row">

                <?php echo $form->labelEx($account, 'address'); ?>

                <?php echo $form->textArea($account, 'address', array('rows' => 4, 'cols' => 50, 'size' => 5, 'maxlength' => 150)); ?>

                <?php echo $form->error($account, 'address'); ?>

            </div>


            <div class="row">

                <?php echo $form->labelEx($account, 'email'); ?>

                <?php echo $form->textField($account, 'email',array('maxlength' => 50)); ?>

                <?php echo $form->error($account, 'email'); ?>

            </div>


            <div class="row">

                <?php echo $form->labelEx($account, 'Landline Number'); ?>

                <? echo 'Country Code: '; ?>

                <?php echo $form->textField($account, 'phone1', array('size' => 5, 'maxlength' => 2)); ?>

                <? echo 'Area Code: '; ?>

                <?php echo $form->textField($account, 'phone2', array('size' => 5, 'maxlength' => 2)); ?>

                <? echo 'Landline Number: '; ?>

                <?php echo $form->textField($account, 'phone3', array('size' => 7, 'maxlength' => 7)); ?>

                <?php echo $form->error($account, 'phone1'); ?>

                <?php echo $form->error($account, 'phone2'); ?>

                <?php echo $form->error($account, 'phone3'); ?>


            </div>


            <div class="row">

                <?php echo $form->labelEx($account, 'Mobile Number'); ?>

                <? echo 'Country Code: '; ?>

                <?php echo $form->textField($account, 'mobile1', array('size' => 5, 'maxlength' => 2)); ?>

                <? echo 'Area Code: '; ?>

                <?php echo $form->textField($account, 'mobile2', array('size' => 5, 'maxlength' => 2)); ?>

                <? echo 'Mobile Number: '; ?>

                <?php echo $form->textField($account, 'mobile3', array('size' => 10, 'maxlength' => 10)); ?>

                <?php echo $form->error($account, 'mobile1'); ?>

                <?php echo $form->error($account, 'mobile2'); ?>

                <?php echo $form->error($account, 'mobile3'); ?>

            </div>

            <div class="row buttons">

                <?php

                echo CHtml::submitButton('Submit', array('onclick' => 'js:function(){ alert("test"); $(#mymodal).dialog("close");}',

                ));

                ?>   

            </div>

            <div>   

    <?php echo CHtml::endForm(); ?>

            </div><!-- form -->

            </p>

            <?php $this->endWidget(); ?>

<?php endif; ?>

        <p><?php echo CHtml::link('Back', array('/account/search')) ?> </p>

    </div>    



Here are is my whole view file, I just copy the first dropdown Status1 just like what you say and change the Model to the name of my model.

Hi again!

I can’t see any error in your code…

Could you check with your browser if really your select boxes values are Account_Status1 and Account_Status2?

It may be Account_status1 and Account_status2 or others …

You can also quote 1 like(not required if not text):




' if($("#Account_Status1").val()=="1"){$("#Account_Status2").attr("disabled",    "disabled"); } 

                          else {$("#Account_Status2").removeAttr("disabled");}'



Good luck friend!