Set field value on Ajax calling model

Hi!

This probably is something very simple for you all; I just can’t figure it out.

I have a form with a dropdown list (named FDCode) in which you can select a value. When a value is selected, another field (named Columns of model Crfdstandard) is supposed to change its value to the value found in the Ajax call.

I found the value using the Ajax call but I cannot get the form field to update to the value found!

This is a part of the form holding the dropdown field which performs the Ajax call:




<div class="row">

   <?php echo $form->labelEx($model3,'FDCode'); ?>

   <?php echo $form->dropDownList($model3,'FDCode', CHtml::listData(FdStandardFormats::model()->findAll(), 'FormatCode', 'FdCode'), 

		array(

			'empty'=> '--Please select--',

			'ajax' => 

			array(

				'type' => 'POST', 

				'url' => CController::createUrl('Add/fetchSFData'), 

				'data'=> array('selected_code'=>'js: $(this).val()'),

				'update' => '#Crfdstandard_Columns',

			)

		)

	);?>

</div>


<td><!-- Columns -->

  <?php echo $form->textField($model3,'Columns',array('size'=>4,'maxlength'=>4)); ?>

</td>			




And this is the Ajax function fetchSFData of AddController.php




public function actionFetchSFData()

{

       // Get the posted value 

	$varFromPost = $_POST['selected_code']; 

	

	// Get the Attributes using the key 

	$data=FdStandardFormats::model()->findByPk($varFromPost); 

		

	$fieldValue = $data->getAttribute("Columns");

		

	// echo $fieldValue;

	//echo CHtml::encode(print_r($fieldValue, true));

	echo Chtml::textField('Columns', $fieldValue);

}



It’s the part where I write the value to the field which I cannot get to work. I have tried different approaches to no avail. Your help will be much appreciated! Thanks in advance!

Hello,

I guess the ‘update’ property is useful for divs, but not inputs, since you don’t assign a content to an input, but a value.

Try with this:

View:


<?php echo $form->dropDownList($model3,'FDCode', CHtml::listData(FdStandardFormats::model()->findAll(), 'FormatCode', 'FdCode'), 

  array(

        'empty'=> '--Please select--',

        'ajax' => array(

                        'type' => 'POST', 

                        'url' => CController::createUrl('Add/fetchSFData'),

                        'data'=> array('selected_code'=>'js: $(this).val()'),

                        'success' => 'js:function(result) {

                                             $("#Crfdstandard_Columns").val(result);

                                      }'

                       )

  )

);?>

Controller:


public function actionFetchSFData()

{

       // Get the posted value 

        $varFromPost = $_POST['selected_code']; 

        

        // Get the Attributes using the key 

        $data=FdStandardFormats::model()->findByPk($varFromPost); 

                

        $fieldValue = $data->getAttribute("Columns");

                

        echo $fieldValue;

}

PS Unless you perform some data manipulation on the selected value, you may just use jQuery to assign the said value to the field you want in the same view. Maybe in your case you can retrieve from the beginning "Columns" value into the selected dropdown… anyway there may be some optimization

That works! Thanks for putting me on the right track!