Yii2 在Yi2中不使用javascript的情况下单击按钮调用控制器操作
我在Yii2 在Yi2中不使用javascript的情况下单击按钮调用控制器操作,yii2,Yii2,我在index2.php文件中有3个字段和一个按钮。这些不是任何型号的。我需要将这些字段作为参数传递,并调用控制器操作stockbetweendates。出于某种原因,我不想使用javascript或ajax。仅凭yii2code如何实现 index2.php <div class="sellitem-index"> <h1><?= Html::encode($this->title) ?></h1> <?php //
index2.php
文件中有3个字段和一个按钮。这些不是任何型号的。我需要将这些字段作为参数传递,并调用控制器操作stockbetweendates。出于某种原因,我不想使用javascript
或ajax
。仅凭yii2
code如何实现
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 <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 <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 <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 <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();
}
公共函数actionStockbetweendates($productname、$startdate、$enddate){
$modelProduction=Puritem::find()->其中(['pi_upc'=>$productname]);
$searchModel1=新的PuritemsbSearch();
$dataProvider1=$searchModel1->search(Yii::$app->request->queryParams,$productname);
$modelSell=Sellitem::find()->其中(['si_iupc'=>$productname]);
$searchModel2=新的SellitemsbSearch();
$dataProvider2=$searchModel2->search(Yii::$app->request->queryParams,$productname);
$content=$this->renderPartial(“\u printproductledgerbtdt”[
“modelProduction”=>$modelProduction,
'dataProvider1'=>$dataProvider1,
“searchModel1”=>$searchModel1,
//“数据”=>$data,
'modelSell'=>$modelSell,
“searchModel2”=>$searchModel2,
'dataProvider2'=>$dataProvider2,
'productname'=>$productname,
//“prodesc”=>$prodesc,
]);
$footer=”
签名
";
$pdf=新pdf([
'mode'=>Pdf::mode_UTF8,
'format'=>Pdf::format_A4,
“目的地”=>Pdf::目的地浏览器,
“方向”=>Pdf::东方景观,
//'destination'=>Pdf::DEST_下载,
“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],
],
“内容”=>$content,
]);
返回$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>
我得到
未定义的偏移量:0
错误。您可以使用GET mthod,因此,您可以从控制器中的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();
您可以使用表单提交数据,这很好。但是如何从文本框中获取数据并作为参数传递给控制器呢?在控制器操作中,我需要传递参数。如果3字段来自您的模型。。您可以使用一个简单的Yi2URL::to和param…它们不是来自任何模型。对不起,我刚才没提。嗨,阿米特,对不起,我没早点通知你。。。字段不是来自任何模型。我得到了错误-未定义的变量模型。您必须为此定义一个模型,并且在该模型中必须添加公共变量或数据库字段。