php - I cannot grab a $_POST value in controller using Yii 2.0 -


using yii 2.0 i'm trying grab $_post values in controller view cannot this. show code , talk through below.

models/casesearch.php

 <?php   namespace app\models;   use yii;  use yii\base\model;  use yii\data\activedataprovider;  use app\models\cases;   /**  * casesearch represents model behind search form `app\models\cases`.  */  class casesearch extends cases {  public $category; public $subcategory; public $childcategory; public $newcategory; /**  * @inheritdoc  */ public function rules() {     return [         [['case_id', 'year'], 'integer'],         [['name', 'judgement_date', 'neutral_citation', 'all_er', 'building_law_r', 'const_law_r', 'const_law_j', 'cill', 'adj_lr'], 'safe'],     ]; }  /**  * @inheritdoc  */ public function scenarios() {     // bypass scenarios() implementation in parent class     return model::scenarios(); }  /**  * creates data provider instance search query applied  *  * @param array $params  *  * @return activedataprovider  */ public function search($params) {     $query = cases::find();      $dataprovider = new activedataprovider([         'query' => $query,     ]);      $this->load($params);      if (!$this->validate()) {         // uncomment following line if not want records when validation fails         // $query->where('0=1');         return $dataprovider;     }      $query->andfilterwhere([          'case_id' => $this->case_id,         'judgement_date' => $this->judgement_date,         'year' => $this->year,     ]);      $query->andfilterwhere(['like', 'name', $this->name])         ->andfilterwhere(['like', 'neutral_citation', $this->neutral_citation])         ->andfilterwhere(['like', 'all_er', $this->all_er])         ->andfilterwhere(['like', 'building_law_r', $this->building_law_r])         ->andfilterwhere(['like', 'const_law_r', $this->const_law_r])         ->andfilterwhere(['like', 'const_law_j', $this->const_law_j])         ->andfilterwhere(['like', 'cill', $this->cill])         ->andfilterwhere(['like', 'adj_lr', $this->adj_lr]);            return $dataprovider; }  public function searchbycategory($category){     $query = cases::find();      $dataprovider = new activedataprovider([         'query' => $query,     ]);        if (!$this->validate()) {         // uncomment following line if not want records when validation fails         // $query->where('0=1');         return $dataprovider;     }      $query->andfilterwhere([          'category_id' => $category      ]);      return $dataprovider; }    } 

okay view:

  <?php       $form = activeform::begin();      echo $form->field($searchmodel, 'category')                 ->dropdownlist(                     arrayhelper::map($allcategory, 'id', 'name'),                     [                         'onchange'=>'getsubcategory()',                     ]     );      //to stop errors, if first category not chosen make subcategory , empty drop down.     $subcategory = array(         "empty" => ""      );      echo $form->field($searchmodel, 'subcategory')                 ->dropdownlist(                     arrayhelper::map($subcategory, 'id', 'name'),                     [                        'onchange'=>'getchildcategory()',                     ]     );     //to stop errors, if second category not chosen make childcategory , empty drop down.     $childcategory = array(         "empty" => ""     );     echo $form->field($searchmodel, 'childcategory')                 ->dropdownlist(                     arrayhelper::map($childcategory, 'id', 'name'),                     [                        //'onchange'=>'getchildcategory()',                         'onchange'=>'submitnow()',                     ]     );        echo '<div class="form-group">';         echo html::submitbutton('submit', ['class' => 'btn btn-primary']);     echo '</div>';     activeform::end(); 

ok, when click submit button want capture value in controller can use alter results given in gridview.

when inspect element on drop down lists names weird not sure if making different. example name subcategory actually: casesearch[subcategory]

now controller:

public function actionindex() { // // first render of index //     $model = new cases;     $searchmodel = new casesearch();       $allcategory = category::find()->all();     $dataprovider = $searchmodel->search(yii::$app->request->queryparams);            //     // when click submit button on view. want submit , grab subcategory in variable $subtest. make $subtest = 1 when first render page doesn't throw error , when submitted should change post value     //       $subtest = 1;     if($searchmodel->load(yii::$app->request->post())){         $subtest = $_post['subcategory'];      }      return $this->render('index', [         'searchmodel' => $searchmodel,         'dataprovider' => $dataprovider,         'allcategory' => $allcategory,         'model' => $model,           'subtest' => $subtest     ]); } 

however when try print_r() variable $subtest in view error:

  undefined index: casesearch[subcategory] 

and line:

  $subtest = $_post['casesearch[subcategory]']; 

in controller.

can please advise cannot figure out why?

i think application design improvable. read yii guide. think solution question is:

  $subtest = $_post['casesearch']['subcategory]; 

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -