简体   繁体   中英

Yii2 post request in controller

I have two submit buttons (submit1 and submit2). When I click "submit2", the controller should write a value (1) in a specific column (abgerechnet) in my db.

   public function actionUpdate($id)
{   
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            if(isset($_POST['submit2']) )
            {
                 $request = Yii::$app->request;
                 $test= $request->post('test', '1');
            }
            return $this->redirect(['view', 'id' => $model->ID]);
        }

        return $this->render('update', [
            'model' => $model,
        ]);

}

But when I click the button "submit2" the column "test" remains empty. With the lines $request = Yii::$app->request; $test= $request->post('test', '1'); $request = Yii::$app->request; $test= $request->post('test', '1'); it should write the value in the column "test".

If you want update the colum abgerechnet in your model based on $_POST['submit2'] then you should set the the value before invoking model->save()

public function actionUpdate($id)
{   
      $model = $this->findModel($id);

      if ($model->load(Yii::$app->request->post()) ) {
          if(isset($_POST['submit2']) )
          {
              $model->abgerechnet = 1;
          }
          $model->save();
          return $this->redirect(['view', 'id' => $model->ID]);
      }

      return $this->render('update', [
          'model' => $model,
      ]);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM