向控制器提交按钮值但未能发布该值
[英] Submitting Button Value to Controller but fail to post the value
本文介绍了向控制器提交按钮值但未能发布该值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道我在这里犯了错误,但我一整天都在寻找这个.我用的是 yii2 框架,我还是新手,我想访问partone/two
页面,partone/two
页面有两个提交按钮,一个是添加行,第二个是验证输入
在 PartoneController.php 内
<代码>user->isGuest) {$this->goHome();}$models = [];$val = "";//创建元素数组Yii::trace("让我们开始吧:");if(Yii::$app->request->post('addRow') == 'true'){Yii::trace("你好");Model::loadMultiple($models, Yii::$app->request->post('items'));$model = new RelationForm();array_push($models, $model);return $this->render('two', ['items' => $models]);}if (Model::loadMultiple($models, Yii::$app->request->post('items')) && Model::validateMultiple($models)){Yii::trace("你好");$count = 0;for($i = 0 ; $i < $models.length(); $i++){if(strpos($relationShownCurrently, '' + $i) !== FALSE){if(!$items[$i]->store()){return $this->render('two', ['items' => $models ]);};}}Yii::$app->session->setFlash('success', "已成功处理 {$count} 条记录.");返回 $this->redirect(['index']);//重定向到下一个想要的页面} 别的 {for($i = 0; $i <5 ; $i++) {$models[$i] = new RelationForm();}return $this->render('two', ['items' => $models]);}返回空;}
里面two.php
registerJsFile('http://code.jquery.com/jquery-2.1.4.min.js');函数 addRelationForm($form, $item, $i){return '<div class="col-md-12" id=\'r_'.($i).'\'><div class="form-group col-md-12"><标签>朋友'.($i + 1) .'</标签>'.$form->field($item, "user_friend_id[$i]") .'<标签>亲近你</label><div class="form-inline">' .$form->field($item, "[$i]closeness")->radio(array("label" => "1", "value" => 1)) .$form->field($item, "[$i]closeness")->radio(array("label" => "2", "value" => 2)) .$form->field($item, "[$i]closeness")->radio(array("label" => "3", "value" => 3)) .$form->field($item, "[$i]closeness")->radio(array("label" => "4", "value" => 4)).$form->field($item, "[$i]closeness")->radio(array("label" => "5", "value" => 5)) .'</div>'.'<div class="form-inline" >我认识这个人是大约(年)的朋友.$form->field($item, "[$i]known_for")->textInput(["type" => "number", "placeholder" => '(in year)'])->;标签(假).'</div>'.'</div></div>';}?><h1>友谊调查</h1><p>内向下方空间,最多列出十位目前在Econs/Math和Econs的最亲密的朋友;至少 5 个是强制性的.*请从提供的下拉列表中选择他们的全名.另外,请在量表上选择您与每个朋友的接近程度.1注意本节的奖励</p><?php $form =ActiveForm::begin(['id' => 'partone-two-form']) ?><?php foreach ($items as $i => $item) {echo addRelationForm($form ,$item, $i);}?><小时><div class="row">**<?= Html::submitButton('添加行', ['name' => 'addMore', 'value' => 'true', 'class' => 'btn btn-info']) ?>
<div style="float:right" class="form-group"><?= Html::submitButton('下一页', ['class' => 'btn btn-primary', 'name' => 'button-value', 'value' => 'next']) ?>
**
<?php ActiveForm::end()?><?php $this->registerJsFile('/advanced/frontend/web/js/partone-two.js');?>
问题说明:
我想确定哪个按钮被触发,我已经指定了提交按钮的名称和值(two.php 中的 addRow 按钮和 nextPage 按钮).但是我无法通过使用 Yii::$app->request->post('addRow') == 'true'
获取 PartoneController.php 中的值,但失败了
我尝试过的其他解决方案:我试图对按钮使用普通的 html,但我不能在控制器 yii 中使用 $_POST
,编译器说
**** 这是我基于 ScaiEdge 反馈的更新代码:****
公共函数 actionTwo(){if(\Yii::$app->user->isGuest){$this->goHome();}$models = [];$val = "";$submit = Yii::$app->request->post('command');for($i = 0; $i <5 ; $i++){$models[$i] = new RelationForm();}//创建元素数组Yii::trace("命令:" .$submit);if($submit == 'addmore'){Yii::trace("你好");Model::loadMultiple($models, Yii::$app->request->post('items'));$model = new RelationForm();array_push($models, $model);return $this->render('two', ['items' => $models]);}if (Model::loadMultiple($models, Yii::$app->request->post()) ){Yii::trace("你好");$count = 0;for($i = 0 ; $i < count($models); $i++){if(!$models[$i]->validate()){if($models[$i]->hasErrors()){Yii::trace(Html::errorSummary($models[$i]));}return $this->render('two', ['items' => $models]);}}for($i = 0 ; $i < count($models); $i++){if(!$models[$i]->store()){return $this->render('two', ['items' => $models]);}}Yii::$app->session->setFlash('success', "已成功处理 {$count} 条记录.");return $this->redirect('three', ['items' => $models]);//重定向到你想要的下一个页面}别的 {return $this->render('two', ['items' => $models, 'submit' => $submit ]);}返回空;}
HTML 客户端:
友谊调查
<p>内向下方空间,最多列出十位目前在Econs/Math和Econs的最亲密的朋友;至少 5 个是强制性的.*请从提供的下拉列表中选择他们的全名.另外,请在量表上选择您与每个朋友的接近程度.1注意本节的奖励</p><?php $form =ActiveForm::begin() ?><?php foreach ($items as $i => $item) {echo addRelationForm($form ,$item, $i);}?><小时><div class="row"><div style="float:left" ><?= Html::submitButton('Add Row', [ 'name' => 'command', 'value' => 'addmore', 'class' => 'btn btn-info']) ?>
<div style="float:right" ><?= Html::submitButton('Next Page', ['class' => 'btn btn-primary', 'name' => 'command', 'value' => 'nextpage']) ?>
<?php ActiveForm::end()?>
当我在客户端删除它时,它起作用了,谁能解释为什么?$item) {echo addRelationForm($form ,$item, $i);}?>
客户端变得像
<?php $form =ActiveForm::begin() ?><小时><div class="row"><div style="float:left" ><?= Html::submitButton('Add Row', [ 'name' => 'chosen', 'value' => 'addmore', 'class' => 'btn btn-info']) ?>
<div style="float:right" ><?= Html::submitButton('Next Page', ['class' => 'btn btn-primary', 'name' => 'chosen', 'value' => 'nextpage']) ?>
<?php ActiveForm::end()?>
addRelationForm 函数:
function addRelationForm($form, $item, $i){return '<div class="col-md-12" id=\'r_'.($i).'\'><div class="form-group col-md-12"><标签>朋友'.($i + 1) .'</标签>'.$form->field($item, "[$i]user_friend_id") .'<标签>亲近你</label><div class="form-inline">' .$form->field($item, "[$i]closeness")->widget(RangeInput::classname(), ['选项' =>['占位符' =>'评分 (1 - 5)...'],'html5Options' =>['分钟' =>1, '最大' =>5、'宽度' =>'75%','插件' =>['附加' =>['内容' =>'<i class="glyphicon glyphicon-star"></i>']]]])->标签(假).'</div>'.'<div class="form-inline" >我认识这个人是大约(年)的朋友.$form->field($item, "[$i]known_for")->textInput(["type" => "number", "placeholder" => '(in year)'])->;标签(假).'</div></div></div>';}
解决方案
你的两个提交按钮的值可以从\Yii::$app->request->post('command')
I know i have made mistakes here but i have been searching for this for the whole day.
I used yii2 framework and i am still new here,
I want to access partone/two
page, the partone/two
page has two submit buttons, one is for adding row, the second one is for validating the input
inside PartoneController.php
<?
public function actionTwo() {
if(\Yii::$app->user->isGuest) {
$this->goHome();
}
$models = [];
$val = "" ;
//Create array of items
Yii::trace("Lets start: ");
if(Yii::$app->request->post('addRow') == 'true'){
Yii::trace("hello");
Model::loadMultiple($models, Yii::$app->request->post('items'));
$model = new RelationForm();
array_push($models, $model);
return $this->render('two', ['items' => $models]);
}
if (Model::loadMultiple($models, Yii::$app->request->post('items')) && Model::validateMultiple($models))
{
Yii::trace("hello again");
$count = 0;
for($i = 0 ; $i < $models.length(); $i++){
if(strpos($relationShownCurrently, '' + $i) !== FALSE){
if(!$items[$i]->store()){
return $this->render('two', [ 'items' => $models ]);
};
}
}
Yii::$app->session->setFlash('success', "Processed {$count} records successfully.");
return $this->redirect(['index']); // redirect to your next desired page
} else {
for($i = 0; $i < 5 ; $i++) {
$models[$i] = new RelationForm();
}
return $this->render('two', ['items' => $models]);
}
return null;
}
inside two.php
<?php
use yii\app\clientScript;
use yii\bootstrap\ActiveForm;
use yii\helpers\Html;
use frontend\models\relation;
$this->registerJsFile('http://code.jquery.com/jquery-2.1.4.min.js');
function addRelationForm($form, $item, $i){
return '<div class="col-md-12" id=\'r_'.($i).'\'>
<div class="form-group col-md-12">
<label> Friend ' . ($i + 1) . '</label>'.
$form->field($item, "user_friend_id[$i]") .
'<label> Closeness to you </label>
<div class="form-inline"> ' .
$form->field($item, "[$i]closeness")->radio(array("label" => "1", "value" => 1)) .
$form->field($item, "[$i]closeness")->radio(array("label" => "2", "value" => 2)) .
$form->field($item, "[$i]closeness")->radio(array("label" => "3", "value" => 3)) .
$form->field($item, "[$i]closeness")->radio(array("label" => "4", "value" => 4)).
$form->field($item, "[$i]closeness")->radio(array("label" => "5", "value" => 5)) .
'</div>'
.'<div class="form-inline" >
I know this person as a friend for approximately (in year) '.
$form->field($item, "[$i]known_for")->textInput(["type" => "number", "placeholder" => '(in year)'])->label(false).
'</div>'.
'</div></div>';
}?>
<h1>Friendship Survey</h1>
<p> Introverted the space below, list up to ten of your closest friends that are currently in Econs/ Math and Econs; a minimum of 5 is compulsory. *Please select their full names from the dropdown list provided. Also, please select on the scale how close you are to each friend. 1Note the incentives for this section </p>
<?php $form =ActiveForm::begin(['id' => 'partone-two-form']) ?>
<?php foreach ($items as $i => $item) {
echo addRelationForm($form ,$item, $i);
}?>
<hr>
<div class="row">
**<div style="float:left" class="form-group">
<?= Html::submitButton('Add row', ['name' => 'addMore', 'value' => 'true', 'class' => 'btn btn-info']) ?>
</div>
<div style="float:right" class="form-group">
<?= Html::submitButton('Next Page', ['class' => 'btn btn-primary', 'name' => 'button-value', 'value' => 'next']) ?>
</div>**
</div>
<?php ActiveForm::end()?>
<?php $this->registerJsFile('/advanced/frontend/web/js/partone-two.js');?>
Problem Explanation:
I want to determine which button is fired, i have specified the name and the value of the submitbutton (addRow button and nextPage button in two.php). But i could not get the value in PartoneController.php by using Yii::$app->request->post('addRow') == 'true'
, but failed
Other Solutions I have tried:
I have tried to use normal html for the button, and i cannot use $_POST
in controller yii, the compler says
**** This is my updated code based on ScaiEdge Feedback:****
public function actionTwo(){
if(\Yii::$app->user->isGuest){
$this->goHome();
}
$models = [];
$val = "" ;
$submit = Yii::$app->request->post('command');
for($i = 0; $i < 5 ; $i++){
$models[$i] = new RelationForm();
}
//Create array of items
Yii::trace("command: " .$submit);
if($submit == 'addmore'){
Yii::trace( "hello");
Model::loadMultiple($models, Yii::$app->request->post('items'));
$model = new RelationForm();
array_push($models, $model);
return $this->render('two', ['items' => $models]);
}
if (Model::loadMultiple($models, Yii::$app->request->post()) )
{
Yii::trace("hello again");
$count = 0;
for($i = 0 ; $i < count($models); $i++){
if(!$models[$i]->validate()){
if($models[$i]->hasErrors()){
Yii::trace( Html::errorSummary($models[$i] ));
}
return $this->render('two', ['items' => $models]);
}
}
for($i = 0 ; $i < count($models); $i++){
if(!$models[$i]->store()){
return $this->render('two', ['items' => $models]);
}
}
Yii::$app->session->setFlash('success', "Processed {$count} records successfully.");
return $this->redirect('three', ['items' => $models]);// redirect to your next desired page
}
else {
return $this->render('two', ['items' => $models, 'submit' => $submit ]);
}
return null;
}
Html Client Side:
<h1>Friendship Survey</h1>
<p> Introverted the space below, list up to ten of your closest friends that are currently in Econs/ Math and Econs; a minimum of 5 is compulsory. *Please select their full names from the dropdown list provided. Also, please select on the scale how close you are to each friend. 1Note the incentives for this section </p>
<?php $form =ActiveForm::begin() ?>
<?php foreach ($items as $i => $item) {
echo addRelationForm($form ,$item, $i);
}
?>
<hr>
<div class="row">
<div style="float:left" >
<?= Html::submitButton('Add Row', [ 'name' => 'command', 'value' => 'addmore', 'class' => 'btn btn-info']) ?>
</div>
<div style="float:right" >
<?= Html::submitButton('Next Page', ['class' => 'btn btn-primary', 'name' => 'command', 'value' => 'nextpage']) ?>
</div>
</div>
<?php ActiveForm::end()?>
When i removed this in the client side, it works, can anyone explain why?
$item) {
echo addRelationForm($form ,$item, $i);
}
?>
the client side become like
<?php $form =ActiveForm::begin() ?>
<hr>
<div class="row">
<div style="float:left" >
<?= Html::submitButton('Add Row', [ 'name' => 'chosen', 'value' => 'addmore', 'class' => 'btn btn-info']) ?>
</div>
<div style="float:right" >
<?= Html::submitButton('Next Page', ['class' => 'btn btn-primary', 'name' => 'chosen', 'value' => 'nextpage']) ?>
</div>
</div>
<?php ActiveForm::end()?>
addRelationForm function:
function addRelationForm($form, $item, $i){
return '<div class="col-md-12" id=\'r_'.($i).'\'>
<div class="form-group col-md-12">
<label> Friend ' . ($i + 1) . '</label>'.
$form->field($item, "[$i]user_friend_id") .
'<label> Closeness to you </label>
<div class="form-inline"> ' .
$form->field($item, "[$i]closeness")->widget(RangeInput::classname(), [
'options' => ['placeholder' => 'Rate (1 - 5)...'],
'html5Options' => [
'min' => 1, 'max' => 5,
'width' => '75%',
'addon' => ['append' => ['content' => '<i class="glyphicon glyphicon-star"></i>']]
]])->label(false).
'</div> '.
'<div class="form-inline" >
I know this person as a friend for approximately (in year) '.
$form->field($item, "[$i]known_for")->textInput(["type" => "number", "placeholder" => '(in year)'])->label(false).
'</div></div></div>';
}
解决方案
The value of your two submit buttons can be obtained from \Yii::$app->request->post('command')
这篇关于向控制器提交按钮值但未能发布该值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文