以ajax形式保存数据yii2

时间:2016-08-31 08:27:57

标签: ajax forms yii2 yii2-advanced-app

我想将数据保存在两个表中。正在使用的表单是带有ajax请求的ajax表单。

我想从表单中获取两个值'Task_ID'和'Employee_ID',然后将其保存到另一个表中。

控制器代码低于

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,
            ]);
        }
    }

}

我需要在保存ajax表单后获取这两个值,或者无论如何我都可以获取值并以ajax形式保存?

任何帮助将不胜感激!谢谢

1 个答案:

答案 0 :(得分:0)

我认为你编写了错误的代码。 不建议(严格地)在[ ... ]语句中执行“=”运算符和方法。 然后,您不会将任何值加载到Activity模型。

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' => '' // ..
    ];

}