douxiangbiao1899
2018-03-27 22:18
浏览 83
已采纳

在Yii2中将数据从一个表单传递到另一个表单

I am currently having problems trying to send data from one form to another form using a button. I have a program where by the client is requesting a new part for their vehicle. The user then applies online and when the part is approved, the user then enter the information into the system. The problem i have is that i am trying to send the relevant data from one form to another that form. Please assist

 <p>
        <?php
        if($model->status=="Approved")
        {
        echo Html::a('Insert into Parts', ['/parts/create?'.$model->lp], ['class' => 'btn btn-primary']) ;
        }
        ?>

    </p>

https://ibb.co/c9rDKn

https://ibb.co/k56kX7

Please view the attached file. On the first image it has the information that is saved into the database also on the top it has a button called insert into part. When the user click the button they are redirected to the send form to insert the part (that is the second image) but i want when the user click the button the information that is related to it is then transferred to the other form. sorry for my english

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duannao1920 2018-03-27 22:44
    已采纳

    In your part controller in create method you should handle to scenario

    GET request: here you will load the vehicle using id submitted using the button then you will populated needed fields in the form.

    POST request: here you will validate your model before saving it and then you will show the view model page if things went well.

    public function actionCreate($id){
      $vehicle = Vehicle::findOne($id);
      $part = new Part();
      //now we have both Vehicle and new part object 
      //based on your desgin if you want to duplicate both your will do this 
      $pary->lincense_plate = $vehicle->lincense_plate;
      //do it for all then the fields will have values if you use ActiveForm
    
      //better desgin to avoid duplication and have a relation one to many between vehicle and part
      //send both models and show vehicle data as disable field or span 
    
      //here we handel post case
      if (Yii::$app->request->isPost) {
        $part->load(Yii::$app->request->post());
        if ($part->save())
          //render view page
          return $this->redirect(['view', 'id' => $part->id]);
      }
      return $this->render('create', [
                'vehicle' => $vehicle,
                'part' => $part,
            ]);
    }
    
    打赏 评论

相关推荐 更多相似问题