Using ajax to update another field - nothing in Post

I’m trying to use a dropDownList to update a text field. I’m using the following code:




<?php echo $form->dropDownList($lineitem,'stock_item_id',$stockItemListData,array(

                'ajax' => array(

                'type'=>'POST', //request type

                'url'=>CController::createUrl('invoiceHeader/dynamicDescription'), //url to call.

                //Style: CController::createUrl('currentController/methodToCall')

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

                //'data'=>'js:javascript statement' 

                //leave out the data key to pass all form values through

                )));  ?>



however nothing seems to be passed to the controller. I’ve looked through Firebug, which I’m using for the first time to try and figure out this issue, and as far as I can tell nothing is being passed in the Post. If I was to put something in the data part what would it be?

Can anyone tell me what I might be missing?

Ok, it’s definitely not passing any data in the Post.

If I change the code to




<?php echo $form->dropDownList($lineitem,'stock_item_id',$stockItemListData,array(

                'ajax' => array(

                'type'=>'POST', //request type

                'url'=>CController::createUrl('invoiceHeader/dynamicDescription'), //url to call.

                //Style: CController::createUrl('currentController/methodToCall')

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

                'data'=>'InvoiceItems[stock_item_id]=8',

                //leave out the data key to pass all form values through

                )));  ?>



(hard coding in the data to pass) it works fine. Can anyone tell me what I need to add to get the value selected from the dropdown into the data? I’m no ajax expert at all.

This will work:


'data'=>'js:{"'.CHtml::activeName($lineitem, 'stock_item_id').'": $("#'.CHtml::activeId($lineitem, 'stock_item_id').'  option:selected" ).val()}',



Awesome, that works! Thanks

Yeah!!

Just remember that Yii is a php framework, not a javascript one. Very often Yii works so awesome that seems like to do all javascript possible without actually writing a single line of js code, but sometimes no.

Remember that:

  • property are escaped, so your js will be spoiled. In order to prevent Yii from distroy your beloved js, use the prefix ‘js:’, that explain to Yii to “attend to his own problems”

  • All name and id are authomatically generated by Yii, you can use "CHtml::activeName" and "CHtml::activeId".

About jquery documentation, you can refer to jquery site but, unfortunately, this documentation is not as good as the Yii one… ask freely in the forum for hemp.