How can i get selected value of a drop down list in _form page to my controller page.
_form page
<?php echo $form->dropDownList($model,'prd_id',CHtml::listData(Category::model()->findAll(),
'cat_id','category')) ?>
I want to get the value of selected item in drop down in my Categorycontroller page.
Try like
<?php echo CHtml::activeDropDownList($model,'prd_id',CHtml::listData(Category::model()- >findAll(),array(
'prompt'=>'Select',
'ajax' => array(
'type'=>'POST',
'url'=>Categorycontroller::createUrl('action'),
)
)); ?>
Sounds like you need AJAX here:
If it is for a dependent dropdown then try this
http://www.yiiframework.com/wiki/24/
Even if it's not for a dependent dropdown the same principals apply with passing the selection to a controller.
echo CHtml::dropDownList('country_id','', array(1=>'USA',2=>'France',3=>'Japan'),
array(
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('currentController/dynamiccities'), //url to call.
//Style: CController::createUrl('currentController/methodToCall')
'update'=>'#city_id', //selector to update
//'data'=>'js:javascript statement'
//leave out the data key to pass all form values through
)));
You can use this in controller:
First use var_dump after $_POST[MODELNAME]
then use
$rs = isset($_POST[MODELNAME]['prd_id']) ? $_POST[MODELNAME]['prd_id'] : array();
$value = (!is_array($rs)) ? explode(',', $rs) : $rs;
Related
I have following code in my view
<?php echo $form->dropDownList($model,'p_28',
CHtml::listData(AdCourt::model()->findAll(),'id','name') ,
array(
'class'=>'form-control',
'style'=>'width:100%;',
'empty'=>Yii::t('plaint','Суд турини танланг'),
'ajax' => array(
'type'=>'POST', //request type
'url'=>$this->createUrl('getcourts'),
'update'=>'#ABlockForm_p_29',
)
)); ?>
AdCourt takes values from ad_court table. This tables consists of id, name and court_id. I need to save database court_id instead of id.
How can I do it?
The second parameter of listData is the attribute of the items that will be used as the value for the <option> tag. As such to use the court_id instead of the id you should pass it instead:
CHtml::listData(AdCourt::model()->findAll(),'court_id','name')
I have a dropdownlist and a textfield.
If the value of dropdownlist is change, I want to call an action in controller and parsing the result to textfield value.
How can I do that?
Here is my View:
echo TbHtml::dropDownList('isr[setting_merit_demerit_id]', $isr['setting_merit_demerit_id'], $this->getMeritList(),
array('class'=>'span4', // 'onchange' => 'generateNumber();',
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('getMeritDemeritStatus'),
// 'success' =>'js:function(result){$("#tes").val(result).change();}', <--- WHAT SHOULD I PUT HERE?
'data' => array('setting_merit_demerit_id' => 'js:this.value'),
)
)
);
echo TbHtml::textField('isr[show_in_reportcard]',$isr['show_in_reportcard'], array('class'=>'span1', 'id'=>'tes'));
Here is my Controller:
function actionGetMeritDemeritStatus(){
$setting_merit_demerit_id = $_POST['setting_merit_demerit_id'];
$model= SettingMeritDemeritM::model()->findByPk($id);
return $model->status;
}
Sorry for my bad english.. thanks
Your first problem is that you are sending an AJAX request and calling a function that just returns a model. You would have to json_encode return value and then echo it. On the receiving end you would have to, in js: success, parse result and then work with that object and assign its properties as values of text fields.
Hope it helps.
I'm using standard Yii dependent dropDownMenu (something similar to this). The logic is that current dropDown will define which element (next dropDown) will be updated with controller action.
echo CHtml::dropDownList('level_1', '', $arrayWithValues, aaray(
'ajax'=>array(
'type'=>'POST',
'url'=>Ccontroller::createUrl('myActionName'),
'update'=>'#level_2'
)));
My question is how can I change the update value after the action is executed? i.e the action find out that we need to populate DropDown Level_3 instead of Level_2.
echo CHtml::dropDownList('level_1', '', $arrayWithValues, aaray(
'ajax'=>array(
'type'=>'POST',
'dataType' => 'json',
'data'=>array('selected'=>'js:this.value'),,
'url'=>Ccontroller::createUrl('myActionName'),
'success'=>'js:function(data){
if (data.update==2){
$("#level_2").empty();
$("#level_2").append(data.items);
}
if (data.update==3){
$("#level_3").empty();
$("#level_3").append(data.items);
}
}'
)));
In your controller you'll have selected value as $_POST['selected'].
public function actionmyaActionName()
{
....
do what you need here
$items.=CHtml::tag('option', array('value'=>$value),CHtml::encode($state),true);
....
echo CJSON::encode(array(
'update'=>$update,
'items'=>$items
));
Yii::app()->end();
}
Form your options, define what to update.
I have a little problem with a Controller method AJAX call in Yii. The thing is that I'm trying to filter the data of one dropDownList based in the value of a previous selected item.
In the view file, where I figured out is the source of the problem, I have this piece of code:
<?php echo $form->labelEx($model,'Estado'); ?>
<?php echo $form->dropDownList($model,'estado',CHtml::listData(Estado::model()->findAll(),'id','nombre'),array(
'ajax'=>array(
'type'=>'POST',
'url'=>CController::createAbsoluteUrl('buscar/select'),
'update'=>'#'.CHtml::activeId($model,'tbl_municipio_id'),
),
'class'=>'form-control'
));
?>
<?php echo $form->error($model,'Estado'); ?>
On the Controller side, I got this:
public function actionSelect(){
echo "Hello world";
$data = Municipio::model()->findAll('tbl_estado_id=:tbl_estado_id',
array(':tbl_estado_id'=>(int) $_POST['Consultorio_estado']));
$data = CHtml::listData($data,'id','name');
foreach($data as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
}
The ajax call to the Select method isn't triggered when the dropDownList is clicked. I tracked the request using Firebug and no error nor fail message is dropped.
Anyone knows what can I do?.
Thanks in advance.
With my knowledge in Yii 1.1.13, there is no such option for ajax for form->dropDownList, just Chtml::dropDownList does.
Therefore you have option to manually custom event change of form->dropDownList or add more jQuery script to handle it by yourself, or simply switch to use Chtml::dropDownList like below example
<?php
echo CHtml::dropDownList('inst_province','',
array(1=>'A',2=>'B',3=>'C', 4=>'D'),
array(
'prompt'=>'Select City',
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('city/selectAll'),
'update'=>'#city_area',
'data'=>array('city_param'=>'js:this.value'),
)));
?>
http://www.yiiframework.com/wiki/429/an-easy-solution-for-dependent-dropdownlist-using-ajax/
I have two combos; provinces and cities.
I would like to change cities value when the province combo value changes. Here is my code
<div class="cities form">
<?php
$v = $ajax->remoteFunction(array('url' => '/cities/','update' => 'divcity'));
print $form-> input('Province.province_id', array('type' => 'select', 'options'=> $provinces, 'onChange' => $v));
?>
<div id="divcity">
<?php
echo $form->input('Cities.cities_name');
?>
</div>
Every time I change province combo, it call cities/index.ctp. anybody want to help?
really thank for your help
wawan
The 'url' => '/cities/' is calling the default index action of the cities controller.
This automatically renders the cities/index.ctp view.
Have you included the RequestHandler component in the cities controller?
This can be used to detect Ajax requests and then render a different view.
You need to first include the RequestHandler Component at the top of the CitiesController, then write a function to list cities, optionally requiring a Province's id.
I think you'll end up having something like this:
<?php
// In the view
$v = $ajax->remoteFunction(array('url' => '/cities/list','update' => 'divcity'));
print $form-> input('Province.province_id', array('type' => 'select', 'options'=> $provinces, 'onChange' => $v));
// In CitiesController
function list($province_id = null) {
// use $this->City->find('list', array('fields'=>array('City.id', 'City.name')))
// to generate a list of cities, based on the providence id if required
if($this->RequestHandler->isAjax()) {
$this->layout = 'ajax';
$this->render();
}
} ?>