Pass Selected Dropdown Option To Controller Through Ajax Button

Hello,

I have searched everywhere and I don’t know if I’m just using the wrong keywords or I’m blind/unlucky, I can’t find this answer.

My problem: I have a pretty complex form, that contains, among other stuff, 2 dropdown fields: one field selects a client id and one selects a year. I also have two tables on the same page that display different data depending on what is selected in those 2 fields. The user should be able to load those tables optionally by pressing a "load data" button.

So, let’s say my 2 dropdowns have ids #year and #client_id, and I have this button




echo CHtml::ajaxButton('Load Data', Yii::app()->createUrl('balance/updateTables'),

            array(

                'data'=> <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />?

                'success' => "js:function(data){ data = JSON.parse(data); $('#table_costs').html(data.costs); $('#table_revenues').html(data.revenues);}",

                'error' => "js:function(){alert('Please select client and year');}",

                )); 

So my problem is I don’t know how to fill ‘data’ passing the 2 selected options to the controller. If I pass some static values like array(‘client_id’=>1, ‘year’=>2013) everything works as expected so the issue is probably just the javascript or the way Yii interacts with that…

I have tried several things, last thing I tried is




'data' => array('year' => 'js:function(){$("#year").on("change", function(){ $("#year option:selected").value; }) }',

                'client_id' => 'js:function(){$("#client_id").on("change", function(){ $("#client_id option:selected").value; }) }'

)



I tried several variants, like without the “on change” parts, using val() instead of value, and others I can’t remember, but no matter what I tried, the button fires a request like /balance/filterData?year=undefined&client_id=undefined&_=1396276817908

So I guess whatever variant I tried there was wrong because it doesn’t get the field value… I’m not really good with javascript so I might be missing something pretty obvious here… I just can’t figure out what, I just can’t wrap my head around it.

Thanks in advance for any help you can give me…

Remove the function and change event and try doing it like this:




array(

	'data' => array(

		'year' => 'js:$("#year").val()',

		'client_id' => 'js:$("#client_id").val()',

	),

	'success' => '...',

	'error' => '...',

)



Ack, I KNEW it had to be simple.

I thought I had tried that form already, probably there was something else wrong when I did.

Thank you so much!

Its not working for me… Can any body please help me how to get it fixed?