php - 如何在 yii 中获取表单元素到 Controller 中

标签 php mysql yii

如何在 yii 中获取表单元素到 Controller 中

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

Controller /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 的值。为什么我遇到这个问题?

最佳答案

您将在 $_POST 中获取表单数据:

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

在你的情况下:

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

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

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

关于php - 如何在 yii 中获取表单元素到 Controller 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22501123/

相关文章:

javascript - 当在 yii google Chart 中传递数组元素时,它传递 null 而不是值

javascript - 在html中使用放大镜将鼠标悬停在图像上

php - mysql sum 函数没有给出结果

javascript - 使用 PHP 将 MySQL 时间以毫秒转换为 javascript 时间戳

mysql 查询 : display all entries from guestbook_comments for a specific owner_id

yii - CHtml::link - 如何添加 html 类?

php - 如何在 Laravel 中获取原始表单数据?

php - 使用javascript或jquery按下按钮时在文本区域中插入图像?

javascript - 通过查询字符串传递变量 chop 加号

php - 有没有办法在不比较属性的情况下比较两个对象?