【问题标题】:Call a controller action on a button click without javascript in yii2在 yii2 中调用没有 javascript 的按钮点击控制器动作
【发布时间】:2017-02-09 11:40:21
【问题描述】:

index2.php 文件中有 3 个字段和一个按钮。这些不是来自任何模型。我需要将字段作为参数传递并调用控制器操作 stockbetweendates。出于某种原因,我不想使用javascriptajax。只用yii2代码怎么办?

index2.php

<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 class="col-xs-4 col-sm-4 col-lg-4">
                <label for="desc" class="control-label"><p class="text-info">Product Desc&nbsp;<i class="icon-star"></i></p></label>
                <input type="text" class="form-control" id="desc" class="span3">
            </div>

    </div>
    <p>
        <div class="form-group pull-right">
            <button id="yourButton" class="btn btn-primary" >Seach</button>           
        </div>
    </p>
</div>

控制器动作

public function actionStockbetweendates($productname, $startdate, $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('_printproductledgerbtdt', [
            '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();

    }

更新 我正在尝试从表单中调用控制器操作并尝试了以下操作 -

<div class="sellitem-form">

    <?php $form = Html::beginForm([
        'id' => 'form-id',
        'method' => 'get',
        'action' => ['/stock/sellitem/printproductledger2']
    ]); ?>

    <div class="row">
        <div class="form-group"> 
            <div class="col-xs-2 col-sm-2 col-lg-2">
                <?= Html::input('text', 'productname', $productname->productname, ['class' => $productname]) ?>
            </div>
        </div>
    </div>

    <div class="form-group">
        <?= Html::submitButton('Search', ['class' => 'btn btn-success']) ?>
    </div>

    <?php Html::endForm(); ?>

</div>

我收到Undefined offset: 0 错误。

【问题讨论】:

  • 可以使用表单提交数据
  • 没关系。但是如何从文本框中获取数据并作为参数传递给控制器​​?在控制器动作中我需要传递参数。
  • 如果 3 字段来自您的模型 .. 您可以使用简单的 Yii2 url::to 和参数 ...
  • 它们不是来自任何型号。抱歉,我之前没有提到。

标签: yii2


【解决方案1】:

您可以使用 GET 方法,因此,您可以从控制器中的 URL 获取参数值。

$form = ActiveForm::begin([
    'id' => 'form-id',
    'method' => 'get',
]);
echo $form->field($Model, 'field1');
echo $form->field($Model, 'field2');
echo $form->field($Model, 'field3');
echo Html::submitButton('Find', ['class' => 'btn btn-primary']);
ActiveForm::end();

【讨论】:

  • 嗨,Amitesh,对不起,我没有早点通知......这些字段不是来自任何模型。我收到错误 - 未定义的变量模型。
  • 您必须为此定义一个模型,并且在该模型中您必须添加公共变量或数据库字段的字段。
猜你喜欢
  • 1970-01-01
  • 2012-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-26
  • 2011-02-20
相关资源
最近更新 更多