如何在yii中将表单元素转换为控制器

时间:2014-03-19 09:20:18

标签: php mysql yii

如何在yii

中将表单元素转换为控制器

视图/ TblRegistration / _form.php这个

<div class="row">
    <?php echo $form->labelEx($model,'director'); ?>
    <?php echo $form->textField($model,'director',array('size'=>50,'maxlength'=>50,'name'=>'txtDirector')); ?>
    <?php echo $form->error($model,'director'); ?>
</div>

<div class="row">
    <?php echo $form->labelEx($model,'experience'); ?>
    <?php echo $form->textField($model,'experience',array('name'=>'txtExp')); ?>
    <?php echo $form->error($model,'experience'); ?>
</div>

<div class="row">
    <?php echo $form->labelEx($model,'language'); ?>
    <?php echo $form->textField($model,'language',array('size'=>50,'maxlength'=>50,'name'=>'txtLang')); ?>
    <?php echo $form->error($model,'language'); ?>
</div>

<div class="row buttons">
<?php echo CHtml::submitButton('Accept', array('name' => 'btnSubmit')); ?>

</div>

控制器/ TblRegistrationController.php

public function actionRegister()
{
    $model=new TblRegistration;

    // Uncomment the following line if AJAX validation is needed
    $this->performAjaxValidation($model);

            if(isset($_POST['btnSubmit']))
            {
                      $dir=isset($_POST['txtDirector']); 
                      $exp=isset($_POST['txtExp']); 
                      $lan=isset($_POST['txtLang']);                                  

                      $cmd=Yii::app()->db->createCommand();
                      $cmd->insert('tbl_registration',
                              array('director'=>$dir,'experience'=>$exp,'language'=>$lan));
                    }       

                    $this->render('register',array('model'=>$model));
}

在数据库中,它将值插入为1 1 1.为什么我遇到此问题?

1 个答案:

答案 0 :(得分:1)

您将获得$ _POST中的表单数据:

$_POST["MODEL_NAME"]["FIELD_NAME"]

在你的情况下:

$_POST["TblRegistration"]["director"]

此外,您没有正确地为变量赋值。请尝试下面的代码

$dir=$_POST['TblRegistration']['txtDirector']; 
$exp=$_POST['TblRegistration']['txtExp']; 
$lan=$_POST['TblRegistration']['txtLang'];