这是我的表单,我将其发送到控制器以获取数据并将其插入带有模型的数据库,但是当我检查控制器中的发布数据时,它为空。这是我的表格:
<div class="container">
<div class="row">
<div class="col-md-12" >
<div class="widget-area no-padding blank">
<div class="status-upload">
<?php
$form = ActiveForm::begin([
'action' => ['twit/send-twit'],
]);
?>
<?= $form->field($model,'twit')->textarea(); ?>
<?= Html::SubmitButton('ارسال',['class' => 'btn btn-success green']); ?>
<?php ActiveForm::end(); ?>
</div><!-- Status Upload -->
</div><!-- Widget Area -->
</div>
</div>
</div>
这是我的控制器,我检查已发布字段的值(twit),它始终为null:
public function actionSendTwit()
{
$request = Yii::$app->request;
var_dump($request->post('twit'));
die();
if ($request->post('twit')){
$twitContent = $request->post('twit');
Twit::sendTwit($twitContent);
\yii::$app->response->redirect('?r=twit/index',301)->send();
}
}
答案 0 :(得分:2)
假设Twit
是型号名称。
public function actionSendTwit()
{
$modelTwit = new Twit();
$request = Yii::$app->request;
if ($modelTwit->load($request->post())) {
$twitContent = $request->post('Twit');
var_dump($twitContent);
Twit::sendTwit($twitContent);
\yii::$app->response->redirect('?r=twit/index',301)->send();
}
}
符合用户要求。
查看强>
现在假设你有两个领域。 username和twit。(我假设这些字段存在于您的数据库表中。)因此,当您使用$twitContent = $request->post('Twit');
时。它使用 Twit模型而不是field name.
如果要检查用户名和twit的值。使用以下方法。
<div class="container">
<div class="row">
<div class="col-md-12" >
<div class="widget-area no-padding blank">
<div class="status-upload">
<?php
$form = ActiveForm::begin([
'action' => ['twit/send-twit'],
]);
?>
<?= $form->field($model,'twit')->textarea(); ?>
<?= $form->field($model,'username')->textinput(); ?>
<?= Html::SubmitButton('ارسال',['class' => 'btn btn-success green']); ?>
<?php ActiveForm::end(); ?>
</div><!-- Status Upload -->
</div><!-- Widget Area -->
</div>
</div>
</div>
<强>控制器强>
<?
public function actionSendTwit()
{
$modelTwit = new Twit();
$request = Yii::$app->request;
if ($modelTwit->load($request->post())) {
$twitContent = $request->post('Twit'); //In this, model is called
//If you want to see value then.
print_r($twitContent['twit']);
print_r($twitContent['username']);
Twit::sendTwit($twitContent);
\yii::$app->response->redirect('?r=twit/index',301)->send();
}
}
?>