0

我的 2 个相关下拉菜单考试类型和状态工作正常,它们显示值..但这些值没有设置到数据库中..请帮助我...这是我的代码

表单视图:


   <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);

}
4

3 回答 3

1

很可能是下拉名称中的问题。你应该更换

CHtml::dropDownList('exam_type'

CHtml::dropDownList(CHtml::activeName($model, 'exam_type'

CHtml::dropDownList('status'

CHtml::dropDownList(CHtml::activeName($model, 'status'
于 2012-06-13T09:36:19.407 回答
1

几天前我也陷入了这个问题。我做了一些工作。

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

在上面的代码中我改变了:

 '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

即使在它不起作用后也发表评论。(我没有测试,也许你的控制器似乎有问题。)

于 2012-06-17T08:17:26.437 回答
0

我希望你的问题已经解决了。如果有人仍然想知道为什么这些下拉列表不更新数据库,他们不会,因为它们没有被列为安全属性。这是使下拉列表起作用的示例(仅给出重要部分):

假设您要添加名为“showOnHome”的字段。首先,在您的数据库表中创建一个名为“showOnHome”的字段(我更喜欢 int(1) 作为字段类型)。

然后,在您的模型 Model 中:

 public function rules() {
....
....
array('showOnHome','safe'),
}

....
....
 public function attributeLabels() {
    return array(
    .....
    .....
    .....
   'showOnHome' => 'Show On Homepage' ,

   )
}

在您的视图中:

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

如果您没有更改 Yii 生成的默认控制器代码,上面的代码片段应该可以完成您的工作,例如

/**
     * 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,
        ));
    }
于 2013-03-13T07:56:07.013 回答