yii中带有submit的下拉列表


dropdown list with submit in yii

我想要一个下拉列表和一个提交按钮,当按下按钮时,我想要获得所选项目的文本并将其存储到数据库中。我的下拉代码是:

<?php
                                $form=$this->beginWidget('CActiveForm', array(
                                'enableAjaxValidation'=>true,
                                'clientOptions'=>array('validateOnSubmit'=>true
                                ),
                                )); ?>

                                <?php echo $form->labelEx($model,'actTopic'); ?>
                                <?php echo $form->error($model,'actTopic'); ?>
                                <?php
                                echo CHtml::dropDownList('actTopic', $category,$list,
                                array('empty' => '... '));
                                ?>

它生成一个下拉列表罚款。但我不知道如何在我的动作函数中获得所选项目。作用函数是:

public function actiongivePoint()
{       
    $model=new GivePointForm;

    if(isset($_POST['GivePointForm'])){
        $model->attributes=$_POST['GivePointForm'];
        if($model->validate()){
            $actTopic=($model->actTopic);
            $actPoint=($model->actPoint);
            $actId=($model->actId);
            $stCode = (int) $_GET['stCode'];
            $connection=Yii::app()->db;
            $connection->active=TRUE;
            $actIdReader =Yii::app()->db->createCommand()
            ->select ('actID')
            ->from('refrencepoint')
            ->where("actTopic=:actTopic")
            ->queryScalar(array(':actTopic'=>$actTopic));
            $search =Yii::app()->db->createCommand()
            ->select ('count(*)')
            ->from('point')
            ->where(array('and', 'actId=:actID', 'stCode=:stCode')  )
            ->queryScalar(array(':actId'=>$actId,':stCode'=>$stCode));          
            if($search !=0){
                $month=CDateFormatter::formatMonth(now());
                $year=CDateFormatter::formatMonth(now());
                $command =$command->update('point', array(
                'year'=>$year,'month'=>$month,
                'pcounter'=>new CDbExpression('pcounter + 1'),),
                (array('and','actId'=>$actId, 'stCode'=>$stCode))) ;
                Yii::app()->user->setFlash('point','....');         
                }   
            else{
                $month=CDateFormatter::formatMonth(now());
                $year=CDateFormatter::formatMonth(now());
                $command->insert('point', array('actId'=>$actId,
                'stCode'=>$stCode,'year'=>$year,'month'=>$month,
                'pcounter'=>new CDbExpression('pcounter + 1')));
                Yii::app()->user->setFlash('point','....');         
    }
}
}                           
    $this->render('givePoint',array('model'=>$model));
}

我应该说我不使用活动记录。

有了上面的代码,看起来一切都很好,但当我按下提交按钮时,什么都没有发生,数据库没有变化,甚至没有错误,只是页面刷新!!

这里怎么了?我已经厌倦了尝试。。。。

我想你只需要像它一样使用。

首先使用foreach循环。我不确定你列出的是哪种类型的东西。但你需要做下面这样的工作。

这是一个例子

foreach($model->category as $list) {
   $list['id'] = $list->name;
}
echo CHtml::dropDownList($model, 'actTopic', $list);

现在您可以调试代码了。

public function actiongivePoint()
{       
$model=new GivePointForm;
if(isset($_POST['GivePointForm'])){
print_r($_POST['GivePointForm']);
exit; //Just check either the form is posted or not and values are coming or not.
$model->attributes=$_POST['GivePointForm'];
if($model->validate()){
$actTopic=($model->actTopic);
echo $actTopic ;
exit ;                 // Check either your dropdown item id is coming or not.

希望它能帮助你。

谢谢。