yii2__
yii2__

Reputation: 177

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'); it should write the value in the column "test".

Upvotes: 1

Views: 1510

Answers (1)

ScaisEdge
ScaisEdge

Reputation: 133360

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,
      ]);

}

Upvotes: 2

Related Questions