How To Update Textfield's Value From Dependent Drop Down

Hi Everybody,

Hope you are all good. I have a problem that is I have a dropdown list which shows number of packages. When I select one package then it’s price should be updated in the following field of the dropdown that is a text field’s value. But unfortunately, after a long try neither I can fix the issue nor find any solution over net. Please someone help me. I pasted my form and controller’s code here.

Form:





    <div class="row">

        <?php echo $form->labelEx($model,'package_id'); ?>

        <?php

        echo $form->dropDownList($model,'package_id',array(),

            array(

                'ajax' =>

                    array('type'=>'POST',

                    'url'=>$this->createUrl('packagePrice'), //url to call.

                    'update'=>'#'.CHtml::activeId($model, 'price'), //selector to update

                    'data'=>array('package_id'=>'js:this.value'),

                )));

        ?>

        <?php echo $form->error($model,'package_id'); ?>

    </div>


	<div class="row">

		<?php echo $form->labelEx($model,'price'); ?>

		<?php echo $form->textField($model,'price',array(),array('size'=>60,'maxlength'=>128)); ?>

		<?php echo $form->error($model,'price'); ?>

	</div>




Controller







    /*

    * action for service package return packages correspondent service

    */

    public function actionPackagePrice(){


        // The following command does not work

        $data = ServicePackage::model()->findByPk(array('id'=>$_POST['package_id']));

        echo $data->price;

    }




Kind regards,

Shimul

Hi

check with firebug on the network section, if the response server answer is the correct

what can you see ?

Hi please see ex…

1)


   echo CHtml::dropDownList('venue_name', '', CHtml::listData(Venue::model()->findAll('status!="2"'), 'id', 'venue_name'), array(

                    'id' => 'send_venue',

                    'prompt' => 'Select Venue',

                    'class' => 'col_165',

                    'ajax' => array(

                        'type' => 'POST',

                        'url' => CController::createUrl('eventname'),

                        'update' => '#event_name',

                        'data' => array('venue_id' => 'js:this.value'),

                        )));

2)


<div id="#event_name"></div>  

3)


public function actionEventname(){


        $venue_id=$_POST['venue_id'];

        $data=VenueEvent::model()->findAll("rk_venue_id='".$venue_id."' AND status='1'");

        $data=CHtml::listData($data,'id','event_name');

       

        foreach($data as $value=>$event_name){

            echo CHtml::tag('option', array('value'=>$value),CHtml::encode($event_name),true);

        }







          

    }

Hi KonApaz,

Thanks to reply. I have checked through firebug and found the response is correct that I got the price value. Just I can’t place that value(price) in the value of textfield correctly.

Firebug result of HTML




<input type="text" maxlength="128" id="Sell_price" name="Sell[price]">150</input>



the value 150 of the above code should be placed in value="150"

Many thanks

Hi Modi,

Thanks to reply. I tried this way but I am little confused how to update.

Shimul

Hi ,

first you can create the the div before the textfield


<div class="update">

<?php echo $form->textField($model,'price',array(),array('size'=>60,'maxlength'=>128)); ?>

</div>

and change the ajax update value


 'update'=>'.update',

i hope it’s work

Yes it makes sense. thank you.

check this

http://www.yiiframework.com/wiki/24/