Multiple submitform in update form in yii2












0















In my Update form, I have two submitform 'Save', 'Save & Submit'. each of them will update particular field.



The submit button looks like -



<?= Html::submitButton('Save',['name'=>'submit', 'value' => 'tello','class' =>  'btn btn-primary']) ?>
<?= Html::submitButton('Save & Submit',['name'=>'submit', 'value' => 'hello','class' => 'btn btn-success']) ?>


The best possible Controller Action I got so far is -



if(Yii::$app->request->post('submit')==='tello'){
var_dump("Save is clicked");
die();
}
elseif(Yii::$app->request->post('submit')==='hello'){
var_dump("Submit is clicked");
die();
}


The problem with this is that, I have to click the button twice to submit it. I learned that I have to change the button name to anything but submit. I tried changing it anything else, but it didn't work. It's negelecting the code and proceeds to the next set of code.



I have this two button in create form as well



form



<?= Html::submitButton('Save',['name'=>'Save', 'value' => 'Save','class' =>  'btn btn-primary']) ?>
<?= Html::submitButton('Save & Submit',['name'=>'Submit', 'value' => 'Submit','class' => 'btn btn-success']) ?>


Controller action



if ($model->load(Yii::$app->request->post())&& isset($_POST['Save'])){
//do my stuff
}
elseif ($model->load(Yii::$app->request->post())&& isset($_POST['Submit'])){
//do my stuff
}


As asked by scaiseEdge Create Controller Action



public function actionCreatenewworkbasic()
{
$model = new Workpermit();

//$model->wp_validfrom = date('Y-m-d H:i:s');
//$model->wp_validto = date('Y-m-d H:i:s');
$model->wp_timeissued = date('Y-m-d H:i:s');

if ($model->load(Yii::$app->request->post()) && isset($_POST['Save'])) {

//get the instance of the uploaded file
//$jhaName = $model->wp_no;
$model->wp_status = 'Saved';
$timenow = date('-Y-m-d-H-i-s');
$model->jha = UploadedFile::getInstance($model,'jha');

if (!empty($model->jha)) {

$model->jha->saveAs('uploads/jha/'.$model->jha->baseName.$timenow.'.'.$model->jha->extension);
//save the path in the db
$model->wp_jhaattach = 'uploads/jha/'.$model->jha->baseName.$timenow.'.'.$model->jha->extension;

}
$model->jha = null;
$model->save(false);


return $this->redirect(['availablework']);
}

elseif ($model->load(Yii::$app->request->post())&& isset($_POST['Submit']))
{
//get the instance of the uploaded file
//$jhaName = $model->wp_no;
$model->wp_status = 'Submitted';
$timenow = date('-Y-m-d-H-i-s');
$model->jha = UploadedFile::getInstance($model,'jha');

if (!empty($model->jha)) {

$model->jha->saveAs('uploads/jha/'.$model->jha->baseName.$timenow.'.'.$model->jha->extension);
//save the path in the db
$model->wp_jhaattach = 'uploads/jha/'.$model->jha->baseName.$timenow.'.'.$model->jha->extension;

}
$model->jha = null;
$model->save(false);

return $this->redirect(['availablework']);
}
else{
return $this->renderAjax('createnewworkbasic', [
'model' => $model,
]);
}


}


Action Update



public function actionUpdatenewwork($id)
{
$anotherchecklistitems = Anotherclreq::find()->select('acl_clname')->where(['acl_wpno'=>$id])->column();


$anotherpermitlistitems = Anotherpermitreq::find()->select('apr_apname')->where(['apr_wpno'=>$id])->column();

$model = $this->findModel($id);
//we have a relation between workpermit and anotherchecklist named anotherchecklistrel
$modelsAnotherchecklist = $model->anotherchecklistrel;
$modelsAnotherpermitlist = $model->anotherpermitlistrel;

if(isset($_POST['Workpermit']['anotherchecklist'])){

$oldIDs = ArrayHelper::map($modelsAnotherchecklist, 'acl_id', 'acl_id');
$oldIDstring = implode(",", $oldIDs);

// print_r($oldIDstring);
// die();

foreach ($oldIDs as $id) {
Anotherclreq::findOne($id)->delete();
}
//die();

$anotherChecklist = $_POST['Workpermit']['anotherchecklist'];
foreach ($anotherChecklist as $value) {

$anotherclreq = new Anotherclreq();
$anotherclreq->acl_wpno = $model->wp_no;
$anotherclreq->acl_clname = $value;
$anotherclreq->save();

}

}

if(isset($_POST['Workpermit']['anotherpermittype'])){

$oldAPs = ArrayHelper::map($modelsAnotherpermitlist, 'apr_id', 'apr_id');
$oldAPstring = implode(",", $oldAPs);
// print_r($oldIDstring);
// die();

foreach ($oldAPs as $id) {
Anotherpermitreq::findOne($id)->delete();
}

$anotherPermit = $_POST['Workpermit']['anotherpermittype'];
foreach ($anotherPermit as $value) {

$anotherpermitreq = new Anotherpermitreq();
$anotherpermitreq->apr_wpno = $model->wp_no;
$anotherpermitreq->apr_apname = $value;
$anotherpermitreq->save();

}

}

if(Yii::$app->request->post('submit')==='tello'){
var_dump("Save is clicked");
die();
}
elseif(Yii::$app->request->post('submit')==='hello'){
var_dump("Submit is clicked");
die();
}

if ($model->load(Yii::$app->request->post())) {
//$model->wp_nwopcheckliststatus = 'Submitted';
$model->save();
return $this->redirect(['viewall', 'id' => $model->wp_no]);
}

return $this->render('updatenewwork', [
'model' => $model,
'anotherchecklistitems' => $anotherchecklistitems,
'anotherpermitlistitems' => $anotherpermitlistitems,
]);
}


In the create form it works good. But for the update form it doesn't. Please help.



Some Observation -
I've changed the buttons like below -



<?= Html::submitButton('Save',['id' => 'savename','name'=>'savename', 'value' => 'savename','class' =>  'btn btn-primary']) ?>
<?= Html::submitButton('Save & Submit',['id' => 'submitname','name'=>'submitname', 'value' => 'submitname','class' => 'btn btn-success']) ?>


Now If I use the following code in Controller action, the form is submitting as it should -



if (Yii::$app->request->isPost) {

if ($model->load(Yii::$app->request->post())) {
$model->wp_nwopcheckliststatus = 'Saved';
$model->save();
return $this->redirect(['viewall', 'id' => $model->wp_no]);
}
}


But as soon as I change $model->load(Yii::$app->request->post()) to $model->load(Yii::$app->request->post('savename')) it stops working.










share|improve this question




















  • 1





    Don't access directly to $_POST variable, you have all your post value into Yii::$app->request->post() Read

    – Sfili_81
    Jan 2 at 9:16













  • update your question and show the code for action create and action update .. and the code for view create and view update

    – scaisEdge
    Jan 2 at 9:53











  • Sffili_81, thanks for the link. It's quite helpful link. scaisEdge, I've added the Controller Action for Create and Update. But the forms you asked for are too large.

    – Alias
    Jan 2 at 10:49











  • what i understand you are trying to submit the same form with 2 different buttons and to 2 different actions inside the controller, your best bet is to use ajax to submit your form to 2 different actions, and you have to change the buttonType from submit to a button changing the name attribute wont make a difference, they would still submit to the same action the form is pointing.

    – Muhammad Omer Aslam
    Jan 2 at 11:44













  • I've a field wp_staus in model... During create I don't want to touch it. It'll have by default status NA. On update form I want to have two buttons. The Save button will write 'Saved' in it. and the 'Save and Submit' button will write 'Submit' in it. I thought, I can do it via controller action only.

    – Alias
    Jan 2 at 14:17
















0















In my Update form, I have two submitform 'Save', 'Save & Submit'. each of them will update particular field.



The submit button looks like -



<?= Html::submitButton('Save',['name'=>'submit', 'value' => 'tello','class' =>  'btn btn-primary']) ?>
<?= Html::submitButton('Save & Submit',['name'=>'submit', 'value' => 'hello','class' => 'btn btn-success']) ?>


The best possible Controller Action I got so far is -



if(Yii::$app->request->post('submit')==='tello'){
var_dump("Save is clicked");
die();
}
elseif(Yii::$app->request->post('submit')==='hello'){
var_dump("Submit is clicked");
die();
}


The problem with this is that, I have to click the button twice to submit it. I learned that I have to change the button name to anything but submit. I tried changing it anything else, but it didn't work. It's negelecting the code and proceeds to the next set of code.



I have this two button in create form as well



form



<?= Html::submitButton('Save',['name'=>'Save', 'value' => 'Save','class' =>  'btn btn-primary']) ?>
<?= Html::submitButton('Save & Submit',['name'=>'Submit', 'value' => 'Submit','class' => 'btn btn-success']) ?>


Controller action



if ($model->load(Yii::$app->request->post())&& isset($_POST['Save'])){
//do my stuff
}
elseif ($model->load(Yii::$app->request->post())&& isset($_POST['Submit'])){
//do my stuff
}


As asked by scaiseEdge Create Controller Action



public function actionCreatenewworkbasic()
{
$model = new Workpermit();

//$model->wp_validfrom = date('Y-m-d H:i:s');
//$model->wp_validto = date('Y-m-d H:i:s');
$model->wp_timeissued = date('Y-m-d H:i:s');

if ($model->load(Yii::$app->request->post()) && isset($_POST['Save'])) {

//get the instance of the uploaded file
//$jhaName = $model->wp_no;
$model->wp_status = 'Saved';
$timenow = date('-Y-m-d-H-i-s');
$model->jha = UploadedFile::getInstance($model,'jha');

if (!empty($model->jha)) {

$model->jha->saveAs('uploads/jha/'.$model->jha->baseName.$timenow.'.'.$model->jha->extension);
//save the path in the db
$model->wp_jhaattach = 'uploads/jha/'.$model->jha->baseName.$timenow.'.'.$model->jha->extension;

}
$model->jha = null;
$model->save(false);


return $this->redirect(['availablework']);
}

elseif ($model->load(Yii::$app->request->post())&& isset($_POST['Submit']))
{
//get the instance of the uploaded file
//$jhaName = $model->wp_no;
$model->wp_status = 'Submitted';
$timenow = date('-Y-m-d-H-i-s');
$model->jha = UploadedFile::getInstance($model,'jha');

if (!empty($model->jha)) {

$model->jha->saveAs('uploads/jha/'.$model->jha->baseName.$timenow.'.'.$model->jha->extension);
//save the path in the db
$model->wp_jhaattach = 'uploads/jha/'.$model->jha->baseName.$timenow.'.'.$model->jha->extension;

}
$model->jha = null;
$model->save(false);

return $this->redirect(['availablework']);
}
else{
return $this->renderAjax('createnewworkbasic', [
'model' => $model,
]);
}


}


Action Update



public function actionUpdatenewwork($id)
{
$anotherchecklistitems = Anotherclreq::find()->select('acl_clname')->where(['acl_wpno'=>$id])->column();


$anotherpermitlistitems = Anotherpermitreq::find()->select('apr_apname')->where(['apr_wpno'=>$id])->column();

$model = $this->findModel($id);
//we have a relation between workpermit and anotherchecklist named anotherchecklistrel
$modelsAnotherchecklist = $model->anotherchecklistrel;
$modelsAnotherpermitlist = $model->anotherpermitlistrel;

if(isset($_POST['Workpermit']['anotherchecklist'])){

$oldIDs = ArrayHelper::map($modelsAnotherchecklist, 'acl_id', 'acl_id');
$oldIDstring = implode(",", $oldIDs);

// print_r($oldIDstring);
// die();

foreach ($oldIDs as $id) {
Anotherclreq::findOne($id)->delete();
}
//die();

$anotherChecklist = $_POST['Workpermit']['anotherchecklist'];
foreach ($anotherChecklist as $value) {

$anotherclreq = new Anotherclreq();
$anotherclreq->acl_wpno = $model->wp_no;
$anotherclreq->acl_clname = $value;
$anotherclreq->save();

}

}

if(isset($_POST['Workpermit']['anotherpermittype'])){

$oldAPs = ArrayHelper::map($modelsAnotherpermitlist, 'apr_id', 'apr_id');
$oldAPstring = implode(",", $oldAPs);
// print_r($oldIDstring);
// die();

foreach ($oldAPs as $id) {
Anotherpermitreq::findOne($id)->delete();
}

$anotherPermit = $_POST['Workpermit']['anotherpermittype'];
foreach ($anotherPermit as $value) {

$anotherpermitreq = new Anotherpermitreq();
$anotherpermitreq->apr_wpno = $model->wp_no;
$anotherpermitreq->apr_apname = $value;
$anotherpermitreq->save();

}

}

if(Yii::$app->request->post('submit')==='tello'){
var_dump("Save is clicked");
die();
}
elseif(Yii::$app->request->post('submit')==='hello'){
var_dump("Submit is clicked");
die();
}

if ($model->load(Yii::$app->request->post())) {
//$model->wp_nwopcheckliststatus = 'Submitted';
$model->save();
return $this->redirect(['viewall', 'id' => $model->wp_no]);
}

return $this->render('updatenewwork', [
'model' => $model,
'anotherchecklistitems' => $anotherchecklistitems,
'anotherpermitlistitems' => $anotherpermitlistitems,
]);
}


In the create form it works good. But for the update form it doesn't. Please help.



Some Observation -
I've changed the buttons like below -



<?= Html::submitButton('Save',['id' => 'savename','name'=>'savename', 'value' => 'savename','class' =>  'btn btn-primary']) ?>
<?= Html::submitButton('Save & Submit',['id' => 'submitname','name'=>'submitname', 'value' => 'submitname','class' => 'btn btn-success']) ?>


Now If I use the following code in Controller action, the form is submitting as it should -



if (Yii::$app->request->isPost) {

if ($model->load(Yii::$app->request->post())) {
$model->wp_nwopcheckliststatus = 'Saved';
$model->save();
return $this->redirect(['viewall', 'id' => $model->wp_no]);
}
}


But as soon as I change $model->load(Yii::$app->request->post()) to $model->load(Yii::$app->request->post('savename')) it stops working.










share|improve this question




















  • 1





    Don't access directly to $_POST variable, you have all your post value into Yii::$app->request->post() Read

    – Sfili_81
    Jan 2 at 9:16













  • update your question and show the code for action create and action update .. and the code for view create and view update

    – scaisEdge
    Jan 2 at 9:53











  • Sffili_81, thanks for the link. It's quite helpful link. scaisEdge, I've added the Controller Action for Create and Update. But the forms you asked for are too large.

    – Alias
    Jan 2 at 10:49











  • what i understand you are trying to submit the same form with 2 different buttons and to 2 different actions inside the controller, your best bet is to use ajax to submit your form to 2 different actions, and you have to change the buttonType from submit to a button changing the name attribute wont make a difference, they would still submit to the same action the form is pointing.

    – Muhammad Omer Aslam
    Jan 2 at 11:44













  • I've a field wp_staus in model... During create I don't want to touch it. It'll have by default status NA. On update form I want to have two buttons. The Save button will write 'Saved' in it. and the 'Save and Submit' button will write 'Submit' in it. I thought, I can do it via controller action only.

    – Alias
    Jan 2 at 14:17














0












0








0








In my Update form, I have two submitform 'Save', 'Save & Submit'. each of them will update particular field.



The submit button looks like -



<?= Html::submitButton('Save',['name'=>'submit', 'value' => 'tello','class' =>  'btn btn-primary']) ?>
<?= Html::submitButton('Save & Submit',['name'=>'submit', 'value' => 'hello','class' => 'btn btn-success']) ?>


The best possible Controller Action I got so far is -



if(Yii::$app->request->post('submit')==='tello'){
var_dump("Save is clicked");
die();
}
elseif(Yii::$app->request->post('submit')==='hello'){
var_dump("Submit is clicked");
die();
}


The problem with this is that, I have to click the button twice to submit it. I learned that I have to change the button name to anything but submit. I tried changing it anything else, but it didn't work. It's negelecting the code and proceeds to the next set of code.



I have this two button in create form as well



form



<?= Html::submitButton('Save',['name'=>'Save', 'value' => 'Save','class' =>  'btn btn-primary']) ?>
<?= Html::submitButton('Save & Submit',['name'=>'Submit', 'value' => 'Submit','class' => 'btn btn-success']) ?>


Controller action



if ($model->load(Yii::$app->request->post())&& isset($_POST['Save'])){
//do my stuff
}
elseif ($model->load(Yii::$app->request->post())&& isset($_POST['Submit'])){
//do my stuff
}


As asked by scaiseEdge Create Controller Action



public function actionCreatenewworkbasic()
{
$model = new Workpermit();

//$model->wp_validfrom = date('Y-m-d H:i:s');
//$model->wp_validto = date('Y-m-d H:i:s');
$model->wp_timeissued = date('Y-m-d H:i:s');

if ($model->load(Yii::$app->request->post()) && isset($_POST['Save'])) {

//get the instance of the uploaded file
//$jhaName = $model->wp_no;
$model->wp_status = 'Saved';
$timenow = date('-Y-m-d-H-i-s');
$model->jha = UploadedFile::getInstance($model,'jha');

if (!empty($model->jha)) {

$model->jha->saveAs('uploads/jha/'.$model->jha->baseName.$timenow.'.'.$model->jha->extension);
//save the path in the db
$model->wp_jhaattach = 'uploads/jha/'.$model->jha->baseName.$timenow.'.'.$model->jha->extension;

}
$model->jha = null;
$model->save(false);


return $this->redirect(['availablework']);
}

elseif ($model->load(Yii::$app->request->post())&& isset($_POST['Submit']))
{
//get the instance of the uploaded file
//$jhaName = $model->wp_no;
$model->wp_status = 'Submitted';
$timenow = date('-Y-m-d-H-i-s');
$model->jha = UploadedFile::getInstance($model,'jha');

if (!empty($model->jha)) {

$model->jha->saveAs('uploads/jha/'.$model->jha->baseName.$timenow.'.'.$model->jha->extension);
//save the path in the db
$model->wp_jhaattach = 'uploads/jha/'.$model->jha->baseName.$timenow.'.'.$model->jha->extension;

}
$model->jha = null;
$model->save(false);

return $this->redirect(['availablework']);
}
else{
return $this->renderAjax('createnewworkbasic', [
'model' => $model,
]);
}


}


Action Update



public function actionUpdatenewwork($id)
{
$anotherchecklistitems = Anotherclreq::find()->select('acl_clname')->where(['acl_wpno'=>$id])->column();


$anotherpermitlistitems = Anotherpermitreq::find()->select('apr_apname')->where(['apr_wpno'=>$id])->column();

$model = $this->findModel($id);
//we have a relation between workpermit and anotherchecklist named anotherchecklistrel
$modelsAnotherchecklist = $model->anotherchecklistrel;
$modelsAnotherpermitlist = $model->anotherpermitlistrel;

if(isset($_POST['Workpermit']['anotherchecklist'])){

$oldIDs = ArrayHelper::map($modelsAnotherchecklist, 'acl_id', 'acl_id');
$oldIDstring = implode(",", $oldIDs);

// print_r($oldIDstring);
// die();

foreach ($oldIDs as $id) {
Anotherclreq::findOne($id)->delete();
}
//die();

$anotherChecklist = $_POST['Workpermit']['anotherchecklist'];
foreach ($anotherChecklist as $value) {

$anotherclreq = new Anotherclreq();
$anotherclreq->acl_wpno = $model->wp_no;
$anotherclreq->acl_clname = $value;
$anotherclreq->save();

}

}

if(isset($_POST['Workpermit']['anotherpermittype'])){

$oldAPs = ArrayHelper::map($modelsAnotherpermitlist, 'apr_id', 'apr_id');
$oldAPstring = implode(",", $oldAPs);
// print_r($oldIDstring);
// die();

foreach ($oldAPs as $id) {
Anotherpermitreq::findOne($id)->delete();
}

$anotherPermit = $_POST['Workpermit']['anotherpermittype'];
foreach ($anotherPermit as $value) {

$anotherpermitreq = new Anotherpermitreq();
$anotherpermitreq->apr_wpno = $model->wp_no;
$anotherpermitreq->apr_apname = $value;
$anotherpermitreq->save();

}

}

if(Yii::$app->request->post('submit')==='tello'){
var_dump("Save is clicked");
die();
}
elseif(Yii::$app->request->post('submit')==='hello'){
var_dump("Submit is clicked");
die();
}

if ($model->load(Yii::$app->request->post())) {
//$model->wp_nwopcheckliststatus = 'Submitted';
$model->save();
return $this->redirect(['viewall', 'id' => $model->wp_no]);
}

return $this->render('updatenewwork', [
'model' => $model,
'anotherchecklistitems' => $anotherchecklistitems,
'anotherpermitlistitems' => $anotherpermitlistitems,
]);
}


In the create form it works good. But for the update form it doesn't. Please help.



Some Observation -
I've changed the buttons like below -



<?= Html::submitButton('Save',['id' => 'savename','name'=>'savename', 'value' => 'savename','class' =>  'btn btn-primary']) ?>
<?= Html::submitButton('Save & Submit',['id' => 'submitname','name'=>'submitname', 'value' => 'submitname','class' => 'btn btn-success']) ?>


Now If I use the following code in Controller action, the form is submitting as it should -



if (Yii::$app->request->isPost) {

if ($model->load(Yii::$app->request->post())) {
$model->wp_nwopcheckliststatus = 'Saved';
$model->save();
return $this->redirect(['viewall', 'id' => $model->wp_no]);
}
}


But as soon as I change $model->load(Yii::$app->request->post()) to $model->load(Yii::$app->request->post('savename')) it stops working.










share|improve this question
















In my Update form, I have two submitform 'Save', 'Save & Submit'. each of them will update particular field.



The submit button looks like -



<?= Html::submitButton('Save',['name'=>'submit', 'value' => 'tello','class' =>  'btn btn-primary']) ?>
<?= Html::submitButton('Save & Submit',['name'=>'submit', 'value' => 'hello','class' => 'btn btn-success']) ?>


The best possible Controller Action I got so far is -



if(Yii::$app->request->post('submit')==='tello'){
var_dump("Save is clicked");
die();
}
elseif(Yii::$app->request->post('submit')==='hello'){
var_dump("Submit is clicked");
die();
}


The problem with this is that, I have to click the button twice to submit it. I learned that I have to change the button name to anything but submit. I tried changing it anything else, but it didn't work. It's negelecting the code and proceeds to the next set of code.



I have this two button in create form as well



form



<?= Html::submitButton('Save',['name'=>'Save', 'value' => 'Save','class' =>  'btn btn-primary']) ?>
<?= Html::submitButton('Save & Submit',['name'=>'Submit', 'value' => 'Submit','class' => 'btn btn-success']) ?>


Controller action



if ($model->load(Yii::$app->request->post())&& isset($_POST['Save'])){
//do my stuff
}
elseif ($model->load(Yii::$app->request->post())&& isset($_POST['Submit'])){
//do my stuff
}


As asked by scaiseEdge Create Controller Action



public function actionCreatenewworkbasic()
{
$model = new Workpermit();

//$model->wp_validfrom = date('Y-m-d H:i:s');
//$model->wp_validto = date('Y-m-d H:i:s');
$model->wp_timeissued = date('Y-m-d H:i:s');

if ($model->load(Yii::$app->request->post()) && isset($_POST['Save'])) {

//get the instance of the uploaded file
//$jhaName = $model->wp_no;
$model->wp_status = 'Saved';
$timenow = date('-Y-m-d-H-i-s');
$model->jha = UploadedFile::getInstance($model,'jha');

if (!empty($model->jha)) {

$model->jha->saveAs('uploads/jha/'.$model->jha->baseName.$timenow.'.'.$model->jha->extension);
//save the path in the db
$model->wp_jhaattach = 'uploads/jha/'.$model->jha->baseName.$timenow.'.'.$model->jha->extension;

}
$model->jha = null;
$model->save(false);


return $this->redirect(['availablework']);
}

elseif ($model->load(Yii::$app->request->post())&& isset($_POST['Submit']))
{
//get the instance of the uploaded file
//$jhaName = $model->wp_no;
$model->wp_status = 'Submitted';
$timenow = date('-Y-m-d-H-i-s');
$model->jha = UploadedFile::getInstance($model,'jha');

if (!empty($model->jha)) {

$model->jha->saveAs('uploads/jha/'.$model->jha->baseName.$timenow.'.'.$model->jha->extension);
//save the path in the db
$model->wp_jhaattach = 'uploads/jha/'.$model->jha->baseName.$timenow.'.'.$model->jha->extension;

}
$model->jha = null;
$model->save(false);

return $this->redirect(['availablework']);
}
else{
return $this->renderAjax('createnewworkbasic', [
'model' => $model,
]);
}


}


Action Update



public function actionUpdatenewwork($id)
{
$anotherchecklistitems = Anotherclreq::find()->select('acl_clname')->where(['acl_wpno'=>$id])->column();


$anotherpermitlistitems = Anotherpermitreq::find()->select('apr_apname')->where(['apr_wpno'=>$id])->column();

$model = $this->findModel($id);
//we have a relation between workpermit and anotherchecklist named anotherchecklistrel
$modelsAnotherchecklist = $model->anotherchecklistrel;
$modelsAnotherpermitlist = $model->anotherpermitlistrel;

if(isset($_POST['Workpermit']['anotherchecklist'])){

$oldIDs = ArrayHelper::map($modelsAnotherchecklist, 'acl_id', 'acl_id');
$oldIDstring = implode(",", $oldIDs);

// print_r($oldIDstring);
// die();

foreach ($oldIDs as $id) {
Anotherclreq::findOne($id)->delete();
}
//die();

$anotherChecklist = $_POST['Workpermit']['anotherchecklist'];
foreach ($anotherChecklist as $value) {

$anotherclreq = new Anotherclreq();
$anotherclreq->acl_wpno = $model->wp_no;
$anotherclreq->acl_clname = $value;
$anotherclreq->save();

}

}

if(isset($_POST['Workpermit']['anotherpermittype'])){

$oldAPs = ArrayHelper::map($modelsAnotherpermitlist, 'apr_id', 'apr_id');
$oldAPstring = implode(",", $oldAPs);
// print_r($oldIDstring);
// die();

foreach ($oldAPs as $id) {
Anotherpermitreq::findOne($id)->delete();
}

$anotherPermit = $_POST['Workpermit']['anotherpermittype'];
foreach ($anotherPermit as $value) {

$anotherpermitreq = new Anotherpermitreq();
$anotherpermitreq->apr_wpno = $model->wp_no;
$anotherpermitreq->apr_apname = $value;
$anotherpermitreq->save();

}

}

if(Yii::$app->request->post('submit')==='tello'){
var_dump("Save is clicked");
die();
}
elseif(Yii::$app->request->post('submit')==='hello'){
var_dump("Submit is clicked");
die();
}

if ($model->load(Yii::$app->request->post())) {
//$model->wp_nwopcheckliststatus = 'Submitted';
$model->save();
return $this->redirect(['viewall', 'id' => $model->wp_no]);
}

return $this->render('updatenewwork', [
'model' => $model,
'anotherchecklistitems' => $anotherchecklistitems,
'anotherpermitlistitems' => $anotherpermitlistitems,
]);
}


In the create form it works good. But for the update form it doesn't. Please help.



Some Observation -
I've changed the buttons like below -



<?= Html::submitButton('Save',['id' => 'savename','name'=>'savename', 'value' => 'savename','class' =>  'btn btn-primary']) ?>
<?= Html::submitButton('Save & Submit',['id' => 'submitname','name'=>'submitname', 'value' => 'submitname','class' => 'btn btn-success']) ?>


Now If I use the following code in Controller action, the form is submitting as it should -



if (Yii::$app->request->isPost) {

if ($model->load(Yii::$app->request->post())) {
$model->wp_nwopcheckliststatus = 'Saved';
$model->save();
return $this->redirect(['viewall', 'id' => $model->wp_no]);
}
}


But as soon as I change $model->load(Yii::$app->request->post()) to $model->load(Yii::$app->request->post('savename')) it stops working.







yii2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 4 at 3:20







Alias

















asked Jan 2 at 8:13









AliasAlias

354529




354529








  • 1





    Don't access directly to $_POST variable, you have all your post value into Yii::$app->request->post() Read

    – Sfili_81
    Jan 2 at 9:16













  • update your question and show the code for action create and action update .. and the code for view create and view update

    – scaisEdge
    Jan 2 at 9:53











  • Sffili_81, thanks for the link. It's quite helpful link. scaisEdge, I've added the Controller Action for Create and Update. But the forms you asked for are too large.

    – Alias
    Jan 2 at 10:49











  • what i understand you are trying to submit the same form with 2 different buttons and to 2 different actions inside the controller, your best bet is to use ajax to submit your form to 2 different actions, and you have to change the buttonType from submit to a button changing the name attribute wont make a difference, they would still submit to the same action the form is pointing.

    – Muhammad Omer Aslam
    Jan 2 at 11:44













  • I've a field wp_staus in model... During create I don't want to touch it. It'll have by default status NA. On update form I want to have two buttons. The Save button will write 'Saved' in it. and the 'Save and Submit' button will write 'Submit' in it. I thought, I can do it via controller action only.

    – Alias
    Jan 2 at 14:17














  • 1





    Don't access directly to $_POST variable, you have all your post value into Yii::$app->request->post() Read

    – Sfili_81
    Jan 2 at 9:16













  • update your question and show the code for action create and action update .. and the code for view create and view update

    – scaisEdge
    Jan 2 at 9:53











  • Sffili_81, thanks for the link. It's quite helpful link. scaisEdge, I've added the Controller Action for Create and Update. But the forms you asked for are too large.

    – Alias
    Jan 2 at 10:49











  • what i understand you are trying to submit the same form with 2 different buttons and to 2 different actions inside the controller, your best bet is to use ajax to submit your form to 2 different actions, and you have to change the buttonType from submit to a button changing the name attribute wont make a difference, they would still submit to the same action the form is pointing.

    – Muhammad Omer Aslam
    Jan 2 at 11:44













  • I've a field wp_staus in model... During create I don't want to touch it. It'll have by default status NA. On update form I want to have two buttons. The Save button will write 'Saved' in it. and the 'Save and Submit' button will write 'Submit' in it. I thought, I can do it via controller action only.

    – Alias
    Jan 2 at 14:17








1




1





Don't access directly to $_POST variable, you have all your post value into Yii::$app->request->post() Read

– Sfili_81
Jan 2 at 9:16







Don't access directly to $_POST variable, you have all your post value into Yii::$app->request->post() Read

– Sfili_81
Jan 2 at 9:16















update your question and show the code for action create and action update .. and the code for view create and view update

– scaisEdge
Jan 2 at 9:53





update your question and show the code for action create and action update .. and the code for view create and view update

– scaisEdge
Jan 2 at 9:53













Sffili_81, thanks for the link. It's quite helpful link. scaisEdge, I've added the Controller Action for Create and Update. But the forms you asked for are too large.

– Alias
Jan 2 at 10:49





Sffili_81, thanks for the link. It's quite helpful link. scaisEdge, I've added the Controller Action for Create and Update. But the forms you asked for are too large.

– Alias
Jan 2 at 10:49













what i understand you are trying to submit the same form with 2 different buttons and to 2 different actions inside the controller, your best bet is to use ajax to submit your form to 2 different actions, and you have to change the buttonType from submit to a button changing the name attribute wont make a difference, they would still submit to the same action the form is pointing.

– Muhammad Omer Aslam
Jan 2 at 11:44







what i understand you are trying to submit the same form with 2 different buttons and to 2 different actions inside the controller, your best bet is to use ajax to submit your form to 2 different actions, and you have to change the buttonType from submit to a button changing the name attribute wont make a difference, they would still submit to the same action the form is pointing.

– Muhammad Omer Aslam
Jan 2 at 11:44















I've a field wp_staus in model... During create I don't want to touch it. It'll have by default status NA. On update form I want to have two buttons. The Save button will write 'Saved' in it. and the 'Save and Submit' button will write 'Submit' in it. I thought, I can do it via controller action only.

– Alias
Jan 2 at 14:17





I've a field wp_staus in model... During create I don't want to touch it. It'll have by default status NA. On update form I want to have two buttons. The Save button will write 'Saved' in it. and the 'Save and Submit' button will write 'Submit' in it. I thought, I can do it via controller action only.

– Alias
Jan 2 at 14:17












1 Answer
1






active

oldest

votes


















0














If I were you, I would create a hidden text field.



and instead of directly submitting the form, I would have attached an event listener.



meaning by javascript, add on click event on the buttons and change the value of hidden text field to anything - 0 - 1 or anything. and then submit the form.



for example:



<input type="hidden" name="pressed" id="pressed" />
<script>

$('#save').click(function(){
$('pressed').val("save");
});

$('#save-and-submit').click(function(){
$('pressed').val("submit");
});

</script>


then check in $_POST['pressed'];






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54003137%2fmultiple-submitform-in-update-form-in-yii2%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    If I were you, I would create a hidden text field.



    and instead of directly submitting the form, I would have attached an event listener.



    meaning by javascript, add on click event on the buttons and change the value of hidden text field to anything - 0 - 1 or anything. and then submit the form.



    for example:



    <input type="hidden" name="pressed" id="pressed" />
    <script>

    $('#save').click(function(){
    $('pressed').val("save");
    });

    $('#save-and-submit').click(function(){
    $('pressed').val("submit");
    });

    </script>


    then check in $_POST['pressed'];






    share|improve this answer




























      0














      If I were you, I would create a hidden text field.



      and instead of directly submitting the form, I would have attached an event listener.



      meaning by javascript, add on click event on the buttons and change the value of hidden text field to anything - 0 - 1 or anything. and then submit the form.



      for example:



      <input type="hidden" name="pressed" id="pressed" />
      <script>

      $('#save').click(function(){
      $('pressed').val("save");
      });

      $('#save-and-submit').click(function(){
      $('pressed').val("submit");
      });

      </script>


      then check in $_POST['pressed'];






      share|improve this answer


























        0












        0








        0







        If I were you, I would create a hidden text field.



        and instead of directly submitting the form, I would have attached an event listener.



        meaning by javascript, add on click event on the buttons and change the value of hidden text field to anything - 0 - 1 or anything. and then submit the form.



        for example:



        <input type="hidden" name="pressed" id="pressed" />
        <script>

        $('#save').click(function(){
        $('pressed').val("save");
        });

        $('#save-and-submit').click(function(){
        $('pressed').val("submit");
        });

        </script>


        then check in $_POST['pressed'];






        share|improve this answer













        If I were you, I would create a hidden text field.



        and instead of directly submitting the form, I would have attached an event listener.



        meaning by javascript, add on click event on the buttons and change the value of hidden text field to anything - 0 - 1 or anything. and then submit the form.



        for example:



        <input type="hidden" name="pressed" id="pressed" />
        <script>

        $('#save').click(function(){
        $('pressed').val("save");
        });

        $('#save-and-submit').click(function(){
        $('pressed').val("submit");
        });

        </script>


        then check in $_POST['pressed'];







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 2 at 18:18









        Vinay SheoranVinay Sheoran

        31339




        31339
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54003137%2fmultiple-submitform-in-update-form-in-yii2%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            Npm cannot find a required file even through it is in the searched directory