php - Get selected value from CHtml::dropDownList
914
I have a question about Yii framework.
Let's say I have dropdown list like the code listed below.
I need to print selected value and I try to do it with$_POST['countries']
but nothing happened.
I just need the selected value to store in a variable so I can manipulate it.
<?php
echo CHtml::dropDownList('countries', $select = array(),
CHtml::listData($countries, 'code', 'name'));
?>
Answer
Solution:
The
$_POST
var is corresponding to some data sent usingHTTP protocol
, so to use this var the client (the browser) need to send data to the server.Since you want to display something just after the user selected a value in the dropdown list, you'll need to use javascript which is executed on the client side.
Here's an example that will get the selected value using jquery. You should then update the div where you want the value to appear, but since it's not in your question I can't help!
Where
dropDownId
is the id that you need to give to your dropDownList (in the CHtml::dropDownList html options)Answer
Solution:
You can also use an Ajax request. Its even better if you want to make some dependent dropdown lists. For example:
Then in your controller you can use POST Example: