将数据从表单传递到 yii2 中的控制器 [英] Pass data from a form to a controller in yii2

查看:29
本文介绍了将数据从表单传递到 yii2 中的控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个包含 3 个字段的页面 - 产品代码、开始日期、结束日期.当我点击搜索按钮时,它应该创建一个 pdf 文件.其中 3 个字段没有模型.

我试过代码 -

title = '股票';$this->params['breadcrumbs'][] = $this->title;?><div class="sellitem-index"><h1><?= Html::encode($this->title) ?></h1><?php//echo $this->render('_search', ['model' => $searchModel]);?><div class="row"><div class="form-group"><div class="col-xs-4 col-sm-4 col-lg-4"><label for="upc" class="control-label"><p class="text-info">产品代码&nbsp;<i class="icon-star"></i></p></label><input type="text" class="form-control" id="upc" class="span3">

<div class="col-xs-4 col-sm-4 col-lg-4"><label for="upc" class="control-label"><p class="text-info">开始日期&nbsp;<i class="icon-star"></i></p></label><?= DatePicker::widget([//'标签' =>'开始日期','名称' =>'开始日期','id' =>'开始日期',//'值' =>'02-16-2012','模板' =>'{插件}{输入}','clientOptions' =>['自动关闭' =>真的,'今天​​亮点' =>真的,'格式' =>'yyyy-mm-dd']]);?>

<div class="col-xs-4 col-sm-4 col-lg-4"><label for="upc" class="control-label"><p class="text-info">结束日期&nbsp;<i class="icon-star"></i></p></label><?= DatePicker::widget([//'标签' =>'开始日期','名称' =>'结束日期','id' =>'结束日期',//'值' =>'02-16-2012','模板' =>'{插件}{输入}','clientOptions' =>['自动关闭' =>真的,'今天​​亮点' =>真的,'格式' =>'yyyy-mm-dd']]);?>

<p><div class="form-group pull-right"><?= Html::a('Search', ['/stock/sellitem/stockbetweendates','upc'=> $upc, 'startdate'=> $startdate, 'enddate'=>$enddate],['class'=>'btn btn-success'])?>

</p>

这是给错误 - 未定义的变量.我在模型中添加了 $upc、$startdate、$enddate,但它没有帮助.请让我知道如何将这些值传递给控制器​​.

控制器动作

public function actionStockbetweendates($upc, $startdate, $enddate) {$upc = yii::$app->request->get('upc');$startdate = yii::$app->request->get('startdate');$enddate = yii::$app->request->get('enddate');//$modelProduction = Puritem::find()->where(['pi_upc' => $productname]);//$searchModel1 = new PuritemsbSearch();//$dataProvider1 = $searchModel1->search(Yii::$app->request->queryParams, $productname);//$modelSell = Sellitem::find()->where(['si_iupc' => $productname]);//$searchModel2 = new SellitemsbSearch();//$dataProvider2 = $searchModel2->search(Yii::$app->request->queryParams, $productname);//$content = $this->renderPartial('_printproductledger', [//'modelProduction' =>$modelProduction,//'dataProvider1' =>$dataProvider1,//'searchModel1' =>$searchModel1,////'数据'=>$数据,//'modelSell' =>$modelSell,//'searchModel2' =>$searchModel2,//'dataProvider2' =>$dataProvider2,//'产品名称' =>$产品名称,//'prodesc' =>$prodesc,//]);//$footer = "////<td style='font-size: 18px;填充底部:20px;'align=\"right\">签名</td>//</tr>//</table>";//$pdf = new Pdf([//'模式'=>PDF::MODE_UTF8,//'格式'=>PDF::FORMAT_A4,//'目的地'=>Pdf::DEST_BROWSER,//'方向'=>Pdf::ORIENT_LANDSCAPE,////'目的地' =>Pdf::DEST_DOWNLOAD,//'cssFile' =>'@vendor/kartik-v/yii2-mpdf/assets/kv-mpdf-bootstrap.min.css',////任何需要嵌入的 css//'cssInline' =>'.kv-heading-1{font-size:18px}',////动态设置 mPDF 属性//'选项' =>['标题' =>'打印派对分类帐'],////'选项' =>['defaultfooterline' =>0,],//'选项' =>['defaultheaderline' =>0,],////即时调用 mPDF 方法//'方法' =>[//'SetHeader'=>['Ledger'],////'SetFooter'=>[$footer],//],//'内容' =>$内容,//]);//返回 $pdf->render();//return $this->render('_printSalarystatement', ['s_period' => $s_period]);}

呈现到 index2 页面的 ActionIndex

公共函数 actionIndex2(){$searchModel = new SellitemSearch();$dataProvider = $searchModel->search(Yii::$app->request->queryParams);返回 $this->render('index2', ['搜索模型' =>$搜索模型,'数据提供者' =>$数据提供者,]);}

解决方案

我不知道不渲染它是如何工作的...但是无论如何,要定义它们,您需要将它们传递给查看文件.最常用的方法(我认为迄今为止最好的)是使用 $this->render() 操作.在 公共函数 actionStockbetweendates($upc, $startdate, $enddate) { 的末尾添加:

return $this->render('index2', ['upc' =>$upc,'开始日期' =>$开始日期,'结束日期' =>$结束日期,];

旁注:您不需要使用这些:

$upc = yii::$app->request->get('upc');$startdate = yii::$app->request->get('startdate');$enddate = yii::$app->request->get('enddate');

它们已经在参数中定义(使用)了.

I am creating a page that has 3 fields - product code, startdate, enddate. When I click on the search button it should create a pdf file. 3 of these fields are without model.

I've tried code -

<?php

use yii\helpers\Html;
//use yii\grid\GridView;
use kartik\grid\GridView;
use kartik\export\ExportMenu;
use frontend\modules\stock\models\Sellitem;
use dosamigos\datepicker\DatePicker;
use dosamigos\datepicker\DateRangePicker;
use kartik\form\ActiveForm;

/* @var $this yii\web\View */
/* @var $searchModel frontend\modules\stock\models\SellitemSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = 'Stock';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="sellitem-index">

    <h1><?= Html::encode($this->title) ?></h1>
    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>

    <div class="row">
        <div class="form-group">
            <div class="col-xs-4 col-sm-4 col-lg-4">
                <label for="upc" class="control-label"><p class="text-info">Product Code&nbsp;<i class="icon-star"></i></p></label>
                <input type="text" class="form-control" id="upc" class="span3">
            </div>
            <div class="col-xs-4 col-sm-4 col-lg-4">
                <label for="upc" class="control-label"><p class="text-info">Start Date&nbsp;<i class="icon-star"></i></p></label>
                <?= DatePicker::widget([
                //'label' => 'Startdate',
                'name' => 'startdate',
                'id' => 'startdate',
                //'value' => '02-16-2012',
                'template' => '{addon}{input}',
                    'clientOptions' => [
                        'autoclose' => true,
                        'todayHighlight' => true,
                        'format' => 'yyyy-mm-dd'
                    ]
                ]);?>
            </div>
            <div class="col-xs-4 col-sm-4 col-lg-4">
                <label for="upc" class="control-label"><p class="text-info">End Date&nbsp;<i class="icon-star"></i></p></label>
                <?= DatePicker::widget([
                //'label' => 'Startdate',
                'name' => 'enddate',
                'id' => 'enddate',
                //'value' => '02-16-2012',
                'template' => '{addon}{input}',
                    'clientOptions' => [
                        'autoclose' => true,
                        'todayHighlight' => true,
                        'format' => 'yyyy-mm-dd'
                    ]
                ]);?>
            </div>
        </div>

    </div>
    <p>
        <div class="form-group pull-right">
            <?= Html::a('Search', ['/stock/sellitem/stockbetweendates','upc'=> $upc, 'startdate'=> $startdate, 'enddate'=>$enddate], 
        ['class'=>'btn btn-success']) 
        ?>
        </div>
    </p>
</div>

This is giving error - undefined variable. I've added $upc, $startdate, $enddate in the model but it's not helping. Please let me know what to do to pass these values to controller.

Controller Action

public function actionStockbetweendates($upc, $startdate, $enddate) {

$upc = yii::$app->request->get('upc');
$startdate = yii::$app->request->get('startdate');
$enddate = yii::$app->request->get('enddate');

// $modelProduction = Puritem::find()->where(['pi_upc' => $productname]);
// $searchModel1  = new PuritemsbSearch();
// $dataProvider1 = $searchModel1->search(Yii::$app->request->queryParams, $productname);

// $modelSell  = Sellitem::find()->where(['si_iupc' => $productname]);
// $searchModel2 = new SellitemsbSearch();       
// $dataProvider2 = $searchModel2->search(Yii::$app->request->queryParams, $productname);

// $content = $this->renderPartial('_printproductledger', [
//     'modelProduction' => $modelProduction,
//     'dataProvider1' => $dataProvider1,
//     'searchModel1'  => $searchModel1,
//     //'data'=> $data,

//     'modelSell' => $modelSell,
//     'searchModel2' => $searchModel2,          
//     'dataProvider2' => $dataProvider2,

//     'productname' => $productname,
//     'prodesc' => $prodesc,

//     ]);
// $footer = "<table name='footer' width=\"1000\">
//    <tr>             
//      <td style='font-size: 18px; padding-bottom: 20px;' align=\"right\">Signature</td>
//    </tr>
//  </table>";
// $pdf = new Pdf([
//     'mode'=> Pdf::MODE_UTF8,
//     'format'=> Pdf::FORMAT_A4,
//     'destination'=> Pdf::DEST_BROWSER,
//     'orientation'=> Pdf::ORIENT_LANDSCAPE,
//     //'destination' => Pdf::DEST_DOWNLOAD,
//     'cssFile' => '@vendor/kartik-v/yii2-mpdf/assets/kv-mpdf-bootstrap.min.css',
//     // any css to be embedded if required
//     'cssInline' => '.kv-heading-1{font-size:18px}', 
//      // set mPDF properties on the fly
//     'options' => ['title' => 'Print Party Ledger'],
//     //'options' => ['defaultfooterline' => 0,],
//     'options' => ['defaultheaderline' => 0,],
//      // call mPDF methods on the fly
//     'methods' => [
//         'SetHeader'=>['Ledger'], 
//         //'SetFooter'=>[$footer],
//     ],
//     'content' => $content,

// ]);
// return $pdf->render();
//return $this->render('_printSalarystatement', ['s_period' => $s_period]);

}

ActionIndex that renders to the index2 page

public function actionIndex2()
    {
        $searchModel = new SellitemSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index2', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
            ]);
    }

解决方案

I'm not sure how it works without rendering... But anyway, to make them defined, you need to pass them to view file. The most common method (and I think the best by far) is to use $this->render() operation. At the end of of your public function actionStockbetweendates($upc, $startdate, $enddate) { add this:

return $this->render('index2', [
    'upc' => $upc,
    'startdate' => $startdate,
    'enddate' => $enddate,
];

One side note: you don't need to use these:

$upc = yii::$app->request->get('upc');
$startdate = yii::$app->request->get('startdate');
$enddate = yii::$app->request->get('enddate');

They are already defined (used) in parameters.

这篇关于将数据从表单传递到 yii2 中的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆