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

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料