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