dynamic value in Active Form

Hi all

I am creating a registration form. In my form, I want to display a dynamic value i.e a value is selected from dropDownList and on the bases of that next attribute value is displayed which is readonly and cannot be changed. Thinking of it as like I am selecting a person name and that person phone number is displayed in next column.

Any suggestions plz…

This is surely what you are searching for :

http://www.yiiframework.com/extension/jquery-cascade

I didn’t tried it, i can’t give you a code example

To make the second select readonly, you may use :




<select >

  <option disabled="disabled" value="volvo">Volvo</option>

  <option disabled="disabled" value="saab">Saab</option>

  <option disabled="disabled" value="mercedes">Mercedes</option>

  <option disabled="disabled" value="audi">Audi</option>

</select>



hey

thanks for the reply, but in my form there is no second select.

I am selecting a person name from dropDownList and its corresponding mobile number is displayed in my registration form.

The mobile number is not supposed to be selected from dropDownList.

Thanks

Say your model name is "User", and it has properties id, name and phone. I do this sort of thing often. This is untested code, but you get the idea:

[php]

// ----------------------------------------------

// User model. Write a function to return the dropdown list.

// ----------------------------------------------

public static function getOptions(l) {

$criteria=new CDbCriteria;

$criteria->select = ‘id, name’; // ONLY select columns needed!

$criteria->order = ‘name’;

$models = self::model()->findAll($criteria);

return CHtml::listData($models, ‘id’, ‘name’);

}

// ----------------------------------------------

// View. This code assumes that your $form is representing

// the selected user with a field called ‘user_id’

// ----------------------------------------------

echo CHtml::activeDropDownList($form, ‘user_id’, User::getOptions(),

array(

            'style' =&gt; 'width:280px;',


            'empty' =&gt; 'Choose user &gt;',


            'ajax' =&gt; array(


                'type' =&gt; 'POST',


                'url' =&gt; &#036;this-&gt;createUrl('selectUser'),


                'dataType' =&gt; 'json',


                'success' =&gt; 'function(data) { selectUser(data);  }',


                'error' =&gt; 'function(data)   { alert(&quot;error&#33;&quot;); }',


                )));

// ----------------------------------------------

// Controller

// ----------------------------------------------

/**

 * Ajax function, called by _form.php view.
  • When a user has been selected, populate the phone number div.

    */

    public function actionClone() {

    &#036;userId = &#036;_POST['UserForm']['user_id'];
    
    
    
    
    
    // Grab User instance
    

    // userId will be null if no user was selected

    if (empty(&#036;userId)) {
    
    
    &#036;event = null;

Hey emily

Thanks for reply.

Your code has been cut at the end and that part I am interested in as I have already implemented the dependent dropDownList earlier.

I hope your code works.

thanks.

Please help me…this is my code:

In _form:




<td>

	<?php echo $form->labelEx($model,'asha_id'); ?></td>

<td>

	<?php echo $form->dropDownList($model,'asha_id',$this->getAsha(),

			array(

				'prompt'=>'Select ASHA',

				'ajax'=>array(

					'type'=>'POST',

					'url'=>CController::createUrl('MotherReg/dynamicasha'),

					'success'=>'function(data) {$("#'.CHtml::activeId($model,'asha_phone').'").val(data);}',

					'error'=> 'function(){alert(\'Bad AJAX\');}',

					'update'=>'#'.CHtml::activeId($model,'asha_phone'),

						)));?>

	<?php echo $form->error($model,'asha_id'); ?></td>


<td>

	<?php echo $form->labelEx($model,'asha_phone'); ?></td>

<td>

	<?php echo $form->textField($model,'asha_phone',array('readonly'=>'readonly')); ?>

	<?php echo $form->error($model,'asha_phone'); ?></td>



In controller:




	public function getAsha()

	{

		$asha = Yii::app()->db->createCommand('select id,name from tbl_asha where subc_id = '.Yii::app()->user->getState('subc'))->queryAll();

		$ashaArray = CHtml::listData($asha,'id','name');

		return $ashaArray;

	}


	public function actionDynamicasha()

	{

		$asha = Asha::model()->findByPk($_POST['MotherReg']['asha_id']);

		echo $asha->phone;

	}