How To Call Controller Action Using Ajax & Pass Parameters Using Ajax To Controller Action

Hii guys,

     I need urgent help plz help.

i have two date fields:




<td>

	

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

		<?php 

			$this->widget('zii.widgets.jui.CJuiDatePicker',array(

				'model'=>$model,

				'name'=>'Mstleave[FromDate]',

				'id'=>'FromDate',

				'attribute'=>'FromDate',

				'options'=>array(

				'showAmin'=>'fold',

				'dateFormat'=>'yy-mm-dd',

				'debug'=>true,),

				'htmlOptions'=>array(

				'size'=>'25',

				'style'=>'height:15px;'

				),

)); ?>


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

	</td>

	


	<td>

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

		<?php 

			$this->widget('zii.widgets.jui.CJuiDatePicker',array(

				'model'=>$model,

				'name'=>'Mstleave[ToDate]',

				'id'=>'ToDate',

				'attribute'=>'ToDate',

				'options'=>array(

				'showAmin'=>'fold',

				'dateFormat'=>'yy-mm-dd',

				'debug'=>true,),

				'htmlOptions'=>array(

				'size'=>'25',

				'style'=>'height:15px;',

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

                              'url'=>$this->createurl('mstleave/getdays'),  ////url to call   

                              'data'=> array('ToDate'=>'js:this.value','FromDate'=>'js:$("#FromDate").val()'),

                              'success'=>'js:function(data) {$("#Mstleave_Duration").val(data); }',)

				),

				)); ?>

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

	</td>



my question is i have written getdays action in my controller. i want that action to call on selection ToDate field & update value to another textfield.

My Controller action is:




public function actionGetdays()

        { 

        	//$date="2014-04-10";

         $sql = 'SELECT datediff('.$_POST['ToDate'].','.$_POST['FromDate'].')';

         $command = Yii::app()->db->createCommand($sql);

         $result = $command->queryScalar(); 

         echo $result ; 

 

         Yii::app()->end(); 

	}      



Hi,

There is an option called onSelect here u will be available with selected date. so u can make ajax call and with response u can update your another text fields

This is sample code





$this->widget(

									'zii.widgets.jui.CJuiDatePicker',

									array(

										'name'=>"[".$index."]studplantime_tdate",

										'model' => $time,

										'attribute' => "[".$index."]studplantime_tdate",

										'language'=> 'de',

										'value'=> $time->studplantime_tdate,

										'options'   => array(

											'dateFormat' => 'dd.mm.yy',

											'disabled'=>false,

											'changeYear'=>true,

											'changeMonth'=>true,

											'onSelect' => 'js:function(selectedDate, inst) {

											// HERE GOES YOUR CODE <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/smile.gif' class='bbc_emoticon' alt=':)' />

					                         }',

										),'htmlOptions'=>array('style'=>'width:65px;height:10px;margin-right:-3px','disabled'=>!$vstatus)

									)

								);