Reputation:
I have been searching for the solution for the past few days but i could not get the correct solution include posting it to stackoverflow before Submitting Button Value to Controller but fail to post the value, but i decided to submit it again because the problem has changed since i already know the error.
I want to post multiple submit button to controller in yii2, at first i can get the submitbutton value from $_POST['chosen'] in controller if the code looks like (below):
<?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()?>
but when i add my function to generate a div, i could not get the $_POST['chosen'] anymore, but i still can get the $model object in controller. the function (addRelationForm) is used to generate div object dynamically so i can submit undertermined-size of array to the controlleer. i could add dynamically the div by pressing add row button.
<?php
use kartik\widgets\RangeInput;
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, "[$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>';
}
?>
<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' => '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()?>
<?php
$this->registerJsFile('/advanced/frontend/web/js/partone-two.js');
?>
my controller looks like this:
public function actionTwo(){
if(\Yii::$app->user->isGuest){
$this->goHome();
}
$models = [];
var_dump($_POST);
Yii::trace("esting" .empty($_POST['chosen']));
for($i = 0; $i < 5 ; $i++){
$models[$i] = new RelationForm();
}
if(!empty($_POST['add'])){
if('addmore' == $_POST['add']){
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()) )
{
$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 {
Yii::trace("Render");
return $this->render('two', array('items' => $models));
}
return null;
}
Upvotes: 2
Views: 1431
Reputation: 40
Instead of <?= Html::submitButton('Add Row', [ 'name' => 'chosen', 'value' => 'addmore', 'class' => 'btn btn-info']) ?>
which create <button type="submit" ... >Submit</button>
use submitInput($label = 'Submit', $options = [])
which create <input type="submit" name="chosen" value="addmore" class="btn btn-info">
Then in controller: $var = \Yii::$app->request->post('chosen');
you can check if variable is empty: empty($var)
(different names for each submitInput with no values) or isset && $var === 'your_value'
(same name, but diff. value in submitInput - not tested if it`s working)
Upvotes: 0