将多个提交按钮值发布到控制器错误

时间:2015-12-13 09:38:27

标签: php yii2 yii2-advanced-app

我一直在寻找过去几天的解决方案,但我无法得到正确的解决方案,包括在Submitting Button Value to Controller but fail to post the value之前将其发布到stackoverflow,但我决定再次提交,因为问题已经改变,因为我已经知道错误。

我想在yii2中向控制器发布多个提交按钮,首先我可以在控制器中获取$ _POST ['选择']的submitbutton值,如果代码如下所示:

    <?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()?>

但是当我添加我的函数来生成div时,我再也无法获得$ _POST [&#39;选择&#39;],但我仍然可以在控制器中获得$ model对象。函数(addRelationForm)用于动态生成div对象,因此我可以向控制器提交欠限大小的数组。我可以通过按添加行按钮动态添加div。

<?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');
?>

我的控制器看起来像这样:

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; 
}

1 个答案:

答案 0 :(得分:0)

而不是创建<?= Html::submitButton('Add Row', [ 'name' => 'chosen', 'value' => 'addmore', 'class' => 'btn btn-info']) ?>的{​​{1}}使用创建<button type="submit" ... >Submit</button>的{​​{1}}

然后在控制器中:submitInput($label = 'Submit', $options = []) 你可以检查变量是否为空:<input type="submit" name="chosen" value="addmore" class="btn btn-info">(每个submitInput没有值的不同名称)或$var = \Yii::$app->request->post('chosen');(相同的名称,但在submitInput中的差异值 - 如果它正在工作则不测试)