Reputation: 39
i have an issue .. i have a form that contain a drop down list that have a lot of values
i want when the user choose specific value from drop down a textarea i shown immediately .. i know i will have to use javascript but i dont know how
this is my Code :
<div class="row">
<?php echo $form->labelEx($model,'type'); ?>
<?php echo $form->dropDownList($model,'type',$model->getTypeOptions()); ?>
<?php echo $form->error($model,'type'); ?>
</div>
<div class="row">
<?php echo "<b>Data</b> : <i> Use WhiteSpaces or , to enter values</i> "; ?><br/>
<?php echo $form->textArea($model, 'data', array('rows'=>10, 'cols'=>50)); ?>
<?php echo $form->error($model, 'data'); ?>
</div>
i want this textarea only shown when the user choose value (ex: checkboxtlist from dropdown)
i searched and found that in
$form->dropDownList($model,'type',$model->getTypeOptions());
we can but array parameter inside dropDownList .. but i dont really know what excatly to write in this array
any help ?
Upvotes: 1
Views: 631
Reputation: 14459
Do like below:
<?php echo $form->dropDownList($model,'type',$model->getTypeOptions(),array('id'=>'myDropDown')); ?>
<?php echo $form->textArea($model, 'data', array('rows'=>10, 'cols'=>50,'id'=>'myTextArea')); ?>
Now, What you need is to create an Ajax request.
<script>
$('#myDropDown').on('change', function() {
$.ajax({
url: "<?php echo $this->createUrl('controller/test'); ?>",
dataType: 'html',
type: 'POST',
data: {dropDownValue: $(this).val()},
success: function(data, textStatus, jqXHR) {
$('#myTextArea').val(data);
}
});
});
Note that test is a action in your controller.(replace with your own names).
Then, Create the action:
public function actionTest() {
if (Yii::app()->request->isAjaxRequest) {
$dropDownValue = Yii::app()->request->getPost('dropDownValue');
if ($dropDownValue === 'A VALUE YOU WANT TO COMPARE WITH') {
echo 'SOME THING YOU WANT';
}
Yii::app()->end();
}
}
To know more, we say that if value of dropdown changed, call an ajax request in order to send dropdown value to server. Then if our value was what we want, we would do something. Finally in success function which means that ajax successfully returned response we change our textArea's value with what server(test action) sent to us.
Upvotes: 2