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 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化