Reputation: 1
my 2 dependent dropdowns exam_type and status are working fine,they displaying values ..but those values are not setting into database..plz help me...this is my code
form view:
<tr>
<td><?php echo $form->labelEx($model,'exam type :'); ?></td>
<td>
<?php echo CHtml::dropDownList('exam_type','',CHtml::listData(class1::model()->findAll(),'class','class'),array('empty'=>'Choose one',
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('dynamicstates'), //url to call.
//Style: CController::createUrl('currentController/methodToCall')
'update'=>'#status', //selector to update
)));
//empty since it will be filled by the other dropdown
?>
</td>
<td> <?php echo $form->error($model,'exam_type'); ?></td>
</tr>
<tr>
<td><?php echo $form->labelEx($model,'status :'); ?></td>
<td><?php echo CHtml::dropDownList('status','', array());?></td>
<td> <?php echo $form->error($model,'status'); ?></td>
</tr>
public function actiondynamicstates()
{
echo $aasd=$_POST['exam_type'];
echo $data=admission::model()->findAll('class=:class',
array(':class'=>$aasd));
$data=CHtml::listData($data,'studentid','studentfname');
foreach($data as $value=>$name)
echo CHtml::tag('option', array('value'=>$value), CHtml::encode($name), true);
}
Upvotes: 0
Views: 3965
Reputation: 3070
I hope your problem got solved already. If someone is still wondering why those drop-downs do not update the database, they don't because they are not listed as safe attributes. Here is an example to make the drop-down-lists work (only important parts are given) :
Suppose you want to add the field called "showOnHome". First, create a field called "showOnHome" in your database table ( I prefer int(1) as the field type).
Then, in your model Model :
public function rules() {
....
....
array('showOnHome','safe'),
}
....
....
public function attributeLabels() {
return array(
.....
.....
.....
'showOnHome' => 'Show On Homepage' ,
)
}
In your View :
<div class="row">
<?php echo $form->labelEx($model,'showOnHome'); ?>
<?php echo $form->dropDownList($model, 'showOnHome', array('0'=>'No','1'=>'Yes') , array('empty'=>'--Select--')); ?>
<?php echo $form->error($model,'showOnHome'); ?>
</div>
The code snippsets above should do your job if you did not alter the default controller code generated by Yii, e.g.
/**
* Creates a new model.
* If creation is successful, the browser will be redirected to the 'view' page.
*/
public function actionCreate()
{
$model=new Livefuture;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Livefuture']))
{
$model->attributes=$_POST['Livefuture'];
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
$this->render('create',array(
'model'=>$model,
));
}
/**
* Updates a particular model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id the ID of the model to be updated
*/
public function actionUpdate($id)
{
$model=$this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Livefuture']))
{
$model->attributes=$_POST['Livefuture'];
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
$this->render('update',array(
'model'=>$model,
));
}
Upvotes: 0
Reputation: 371
some days before i too got stuck in this problem.i did some trick to work.
<tr>
<td><?php echo $form->labelEx($model,'exam type :'); ?></td>
<td>
<?php echo CHtml::dropDownList('exam_type','',CHtml::listData(class1::model()->findAll(),'class','class'),array('empty'=>'Choose one',
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('dynamicstates'), //url to call.
//Style: CController::createUrl('currentController/methodToCall')
// 'update'=>'#status', //selector to update
'update'=>'#modelname_status', //here is the trick replace modelname to orginal model class name
)));
//empty since it will be filled by the other dropdown
?>
</td>
<td> <?php echo $form->error($model,'exam_type'); ?></td>
</tr>
<tr>
<td><?php echo $form->labelEx($model,'status :'); ?></td>
<?php //echo CHtml::dropDownList('status','', array());?>
<td><?php echo CHtml::dropDownList('modelname[status]','', array());?></td>//same here
<td> <?php echo $form->error($model,'status'); ?></td>
</tr>
in above code i changed:
'update'=>'#modelname_status', //here is the trick replace modelname to orginal model class name
<td><?php echo CHtml::dropDownList('modelname[status]','', array());?></td>//same here
even after it doesnt work give comment.(i didnt test,maybe your controller seems have problem.)
Upvotes: 1
Reputation: 233
It most likely that problem in dropdowns names. You should replace
CHtml::dropDownList('exam_type'
with
CHtml::dropDownList(CHtml::activeName($model, 'exam_type'
And
CHtml::dropDownList('status'
with
CHtml::dropDownList(CHtml::activeName($model, 'status'
Upvotes: 1