Dropdownlist Populating Other Textfiled

Hello to all, I hope this was the right section for my question.

I write a drop down list populated from values of another model


<?php echo $form->dropDownList($model,'idcausale_uscita', CHtml::listData(CausaleUscita::model()->findAll(), 'idcausale_uscita', 'uscita')); ?>

Is it possible to change the value of another textfield, in the same form, accordling the value of dropdownlist chosen? I can’t figure out… maybe with onchange event?

$model,‘idcausale_uscita’ is the first model and refer to current form. The second ‘idcausale_uscita’ (not a good idea give it the same name…), ‘uscita’ are the values populating dropdownlist of another model.

The table with data to select are very simple:

id – description – nvalue

0 – aaa – 10

1 – bbb – 12

2 – ccc – 5

… and so on

I need to select from description field (and this work), and put in another textfield the corrisponding “nvalue”. And this I can’t figure out.

Thank you in advance

[color="#006400"]/* moved from Miscellaneous [/color][color="#006400"] */[/color]

To clarify what I try to do.

I want to update a textField in a CActiveForm when I choose a value from a Dropdownlist. The model is linked to database. But some steps is not so clear for me.

This is the relevant part in _form.php


	<div class="row">

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

		<?php

		//Dropdowlist 

		$dati = CHtml::listData(CausaleUscita::model()->findAll(), 'idcausale_uscita', 'uscita');

		$fupdate = array( 'empty'=>' Selezionare una voce ',

       				'ajax'=>array(

        					'type'=>'POST',

        	  				'url'=>CController::createUrl('Spesa/update_prezzo'),

        	  				'replace'=>'#Spesa_importo', // With "update" doesn't work

						)

				);

		echo $form->dropDownList($model,'idcausale_uscita', $dati, $fupdate);

		?>


	<div class="row">

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

		<?php 


			echo $form->textField($model,'importo', array('size'=>10,'maxlength'=>10));

		?>

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

	</div>



This is SpesaController.php


    public function actionUpdate_prezzo()

   {

	

	//Only work with "replace" function in Dropdownlist

	echo CHtml::textField('Spesa[importo]','123', array('id'=>'Spesa_importo','size'=>10,'maxlength'=>10)); //"123" Is arbitrary value, just to try if it work.


   }

First question. The Dropdownlist in the form works fine, read data from database and it is corrected populated. The controller, in this way, works too, but I’m not shure this is the more logical way to do this work. But with “update” option, instead “replace”, I can’t save data in database. Is there a way to simply set the ‘importo’ value in CActiveForm with controller function?

Second question. Can you put me in the right direction to substitute the manual value "123" with a variable pupulated by a field from a record selected by Dropdownlist?

Thank you in advance.

This functionality is not natively provided by the framework. Anyway, its very easy to achieve that once you look at the source code of CHtml::ajax()

_form.php




[...]

$fupdate = array(

	'empty' => ' Selezionare una voce ',

	'ajax' => array(

		'type'    => 'POST',

		'url'     => $this->createUrl('Spesa/update_prezzo'),

		'success' => new CJavaScriptExpression('function(html){jQuery("#'.CHtml::activeId($model, 'importo').'").attr("value", html)}'),

	)

);

[...]



SpesaController.php




public function actionUpdate_prezzo()

{

	echo 'This text will show up as the textbox value';

}



Once again: Checking the source code of CHtml::ajax() reveals this:


if(!isset($options['data']) && isset($options['type']))

        $options['data']=new CJavaScriptExpression('jQuery(this).parents("form").serialize()');

So you have full access to the user entered values.

SpesaController.php




public function actionUpdate_prezzo()

{

	echo Spesa::model()->findByPk($_POST['Spesa']['id']))->importo;

}



Thank you for your kindly reply.

You fully answered at my first question, and your solution works fine. But I will lie if I tell you that all is clear now. This code


 'success' => new CJavaScriptExpression('function(html){jQuery("#'.CHtml::activeId($model, 'importo').'").attr("value", html)}'), 

is still criptic for me and the source code of CHtml::ajax() help me only to feel me more stupid than ever. :slight_smile: If you can point me to some guide, book or manual that can be useful (jquery?, ajax?) to learn this, it will be very appreciate.

The solution for second answer doesn’t work, and I will investigate. (Parenthesis error, and after deleted one parenthesis “Undefined index: id” error.)

Thank you again for your time and for your precious help.

I do not know about a good jQuery tutorial, but I guess there a dozen’s of it in the WWW. I can just refer you to the offical documentation for the jQuery ajax function: http://docs.jquery.com/Ajax/jQuery.ajax#options

About the "Undefined index" error: Unless i know your fomular i can only guess which data is submitted on the ajax request… Just use var_dump($_POST) to find out - or view the source code of your formular.

The second solution for me works too


public function actionUpdate_prezzo()

{

        echo CausaleUscita::model()->findByPk($_POST['Spesa']['idcausale_uscita'])->importo;

}

Thank you again