Ajax Calling Values Couldn't Save Into A Variable In Controller

Hi Everybody,

Hope you are all good. I have a problem that is I use an extension i.e. select2 which shows all Products’ name in the dropdown list and it works fine to me. In my database, every product has individual price. So, I want to show the sum of all selected products’ price in the next of dropdown. So, I used ajax to send the product id to the controller to find the product price. Unfortunately, I can’t sum all of product price because when I add new product then previous Id of product gets change then price also gets change. But I want to hold the price of products in a variable when product is selected.

the form code:

<div class="row">

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


        $data = CHtml::listData(Products::model()->findAll(),'id', 'name');

        echo Select2::activeMultiSelect(

            $model, 'item_list', $data, array(

                'required' => 'required',

                'style' => 'width: 270px;',

                'placeholder' => 'Add Product',

                'select2Options' => array(),

                'ajax' => array('type'=>'POST',

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

                    'update'=>'#price', //selector to update






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


the controller code:



    public $prices;



    public function actionTotalOrderPrice(){

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

        $this->prices += $data->prices;

        echo CHtml::tag('input', array( 'type'=>'text' , 'name'=>'Order[price]' , 'value' => $this->prices));


I spent a lot of time to solve this problem but anyhow couldn’t. Please help someone.

Thanks in advance,


Check the url path with firebug console

this should be like this way.


Hi Balu,

Thanks to answer me. I do not understand clearly what you actually mean. Could you explain more for me please. Actually I have no problem to receive the product id through ajax. I saw through firebug console that product id reaches at controller. I retrieve the product price by that id but unfortunately I couldn’t sum all the product price because when I add new product, controller receives new product id and price is being changed. Please could you help me how the way should be to hold product price in variable once multiple product would be added.


url’=>$this->createUrl(‘totalOrderPrice’), //url to call.

----- this line in oyu above code should be like this

   url'=&gt;&#036;this-&gt;createUrl('Controller/action'), //url to call.

   here your action is totalOrderPrice , you have name the controller's name also..

   watch this video http://www.youtube.com/watch?v=VHOg_Ks-a5E

     you get clear idea to debug this code....

Dear Balu,

I have reached at what you said. I have attached a screenshot actually what I’m looking for. Please check. It would be really a great help for me.


In each time while the action is performing "totalOrderPrice" insert that value in db, and inn every action it should check in db is any value is present in db , id is value is present it should be added to the current value… After submitting the form the db should be cleared… i think this may solve your issue.

Thanks for your help, I did it in different way.

Ok fine…

You can replace the ajax ‘update’ by

'success' => 'function(returnajax) {

    var oldvalue = parseFloat($('#idofinput').val()),

         newvalue = parseFloat(returnajax);

    $("#result").val(oldvalue + newvalue);


Thanks Tonin, Iĺl try