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
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.
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