gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:17:23 26 4
gpt4 key购买 nike

如何在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com