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

在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,
            ]);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看