2015-12-10 13 views
6

Tôi biết tôi đã phạm sai lầm ở đây nhưng tôi đã tìm kiếm điều này cho cả ngày. tôi đã sử dụng yii2 khuôn khổ và tôi vẫn còn mới đây, tôi muốn truy cập partone/two trang, trang partone/two có hai nộp nút, một là để thêm liên tiếp, điều thứ hai là để phê chuẩn đầu vàoGửi giá trị nút cho bộ điều khiển nhưng không đăng giá trị

bên 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; 
} 

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

Vấn đề Giải thích:

Tôi muốn xác định nút đó là bắn, tôi đã xác định tên và giá trị của submitbutton (nút addRow và nút Nextpage trong two.php). Nhưng tôi không thể nhận được giá trị trong PartoneController.php bằng cách sử dụng Yii::$app->request->post('addRow') == 'true', nhưng thất bại

Solutions khác Tôi đã thử: Tôi đã cố gắng sử dụng html bình thường đối với các nút, và tôi không thể sử dụng $_POST trong điều khiển yii, các compler nói

**** Đây là mã cập nhật của tôi dựa trên ScaiEdge phản hồi: ****

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

Khi tôi xóa mục này ở phía máy khách, nó hoạt động, bất cứ ai có thể giải thích lý do tại sao? $ item) { echo addRelationForm ($ form, $ item, $ i); ? } >

phía khách hàng trở nên giống như

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

chức năng addRelationForm:

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

    } 

Trả lời

0

Bạn đang sử dụng hai nút nộp, vì vậy cuộc xung đột xảy ra ở giữa hai nộp nút. Thử thay đổi Yii :: $ app-> request-> post ('addRow') thành Yii :: $ app-> request-> post ('addmore') cho trang tiếp theo try Yii :: $ app-> request -> bài đăng ('trang tiếp theo')

Các vấn đề liên quan