Updating values of a dropdown list with javascript

Hi,

I have a form with $.post which returns 2 arrays as result. The values of one array are used in some calculation which thereafter updates some values in the form. I wish to update a variable $room in the form which is used as a dropdown list. Can anyone help me, please?

Here is the code:





...

...

...

<?php Yii::app()->getClientScript()->registerScript('duration','$("#arr,#dep").change(function()

	{


				$.get("index.php?r=reservation/Calcul", {brd: $("#brd").val(),rmtype: $("#rmtype").val(), nbr:nombre, dt:$("#arr").val() }, function(data) 

				{

					var amt = nb*data.prix;

					adad = $("#add_ad").val();

					adch = $("#add_ch").val();

					amt = amt+(nb*adad*data.prixadd)+(nb*adch*data.prixadd/2);

					$("#amt").val(amt);

					var myarr = data.test;

					amt = nb*myarr["two"];

					$("#amt").val(amt);				

					$("#test").val(myarr);

				},"json");

			}

	}

	);');

?>

...

...

...


		<?php echo $form->dropDownList($model,'room',$room);?>



I guess it could be something quite simple and stupid :)

Thanks

Your approach can’t work, you have to understand that your php page is generated on the server, then sent to the client where he changes some fields which generate a request to the server which sends back an array. And there it ends, you can either use javascript to update the client “static” html DOM, or improve your server code to actually generate more than an array, but the full updated dropdown.

The PHP approoach can be handle in Yii using renderPartials. Create a view _myDropdown.php, and pass it as the $room array as parameter.

Then when your text fields are updated (use http://www.yiiframework.com/doc/api/1.1/CHtml#textField-detail and onChange via the clientChange method : http://www.yiiframework.com/doc/api/1.1/CHtml#clientChange-detail ), make it call a controller action which will make the calculation and re-renderPartial your dropdown view.

On a sidenote, I’d be carefull with price calculation on the clientside ;)

Thanks for your reply.

If I already have the required values for the dropdown list in the array returned by Jquery $.post, is it not possible to retrieve these values in the view form in the same way I retrieve values by the command of the following type:

$("#amt").val(amt);

Can you give me an example of what you suggested? Thanks

Hi,

maybe this wiki could help you:

http://www.yiiframework.com/wiki/24/creating-a-dependent-dropdown

That’s not exactly the same problem as yours, but some elements can interest you.

Thanks Ragua,

I will have a look at it. Meanwhile I am trying to do it another way but having problem with undefined index in my code.

Here is the link to my thread:

http://www.yiiframework.com/forum/index.php/topic/33342-help-with-undefined-index/page__gopid__160497#entry160497