weixin_33736649 2016-08-31 08:27 采纳率: 0%
浏览 20

将数据以ajax格式yii2保存

I want to save data in two tables. The form being used is an ajax form with ajax request.

I want to obtain two values 'Task_ID' and 'Employee_ID' from the form and then save it to another table.

Controller Code is below

public function actionCreate()
{
    $request = Yii::$app->request;
    $model = new Activity();  
    $empModel = new Tasksemp();
    //$tasksModel = new Tasks();

    if($request->isAjax){
        /*
        *   Process for ajax request
        */
        Yii::$app->response->format = Response::FORMAT_JSON;
        if($request->isGet){



            return [
                'title'=> "Create new Activity",
                'content'=>$this->renderAjax('create', [

                    'model' => $model,

                    $model->finish_date = date('y-m-d'), //give default date as current along with date picker option

                    $empModel->Task_ID = $model->Task_ID, //<----Error here. The value of Task_ID and Employee_ID is null.
                    $empModel->Employee_ID = $model->Employee_ID,
                    $empModel->save(false),

                ]),
                'footer'=> Html::button('Close',['class'=>'btn btn-default pull-left','data-dismiss'=>"modal"]).
                            Html::button('Save',['class'=>'btn btn-primary','type'=>"submit"])

            ];





        }else if($model->load($request->post()) && $model->save()){
            return [
                'forceReload'=>'#crud-datatable-pjax',
                'title'=> "Create new Activity",
                'content'=>'<span class="text-success">Create Activity success</span>',
                'footer'=> Html::button('Close',['class'=>'btn btn-default pull-left','data-dismiss'=>"modal"]).
                        Html::a('Create More',['create'],['class'=>'btn btn-primary','role'=>'modal-remote'])

            ];         
        }else{           
            return [
                'title'=> "Create new Activity",
                'content'=>$this->renderAjax('create', [
                    'model' => $model,
                    'empModel' => $empModel,
                ]),
                'footer'=> Html::button('Close',['class'=>'btn btn-default pull-left','data-dismiss'=>"modal"]).
                            Html::button('Save',['class'=>'btn btn-primary','type'=>"submit"])

            ];         
        }           
    }else{
        /*
        *   Process for non-ajax request
        */
        if ($model->load($request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->Activity_ID]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

}

I need to obtain the two values after the ajax form has been saved or anyway way I can get the values and save in the ajax form?

Any help would be appreciated! Thank you

  • 写回答

1条回答 默认 最新

  • 斗士狗 2016-08-31 19:09
    关注

    I think you wrote incorrect code. Is not recommend (strictly) perform "=" operator and methods within [ ... ] statement. Then you not load any values to Activity model.

    if ( $request->isGet ){
    
        $model->finish_date = date('y-m-d'); //give default date as current along with date picker option
    
        // loading values from form
    
        $empModel->Task_ID      = $request->get('name_of_form_input_that_contains_Task_ID'); 
        $empModel->Employee_ID  = $request->get('name_of_form_input_that_contains_Employee_ID');
    
        $empModel->save(false);
    
        return [
            'title'   => "Create new Activity",
            'content' => $this->renderAjax('create', [
                'model' => $model,
                // any other data
            ]),
            'footer' => '' // ..
        ];
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用百度地图api 位置函数报错?
  • ¥15 metamask如何添加TRON自定义网络
  • ¥66 关于川崎机器人调速问题
  • ¥15 winFrom界面无法打开
  • ¥30 crossover21 ARM64版本安装软件问题
  • ¥15 mymetaobjecthandler没有进入
  • ¥15 mmo能不能做客户端怪物
  • ¥15 osm下载到arcgis出错
  • ¥15 Dell g15 每次打开eiq portal后3分钟内自动退出
  • ¥200 使用python编写程序,采用socket方式获取网页实时刷新的数据,能定时print()出来就行。