1

I am working on yii2. I am trying to get the submit button name in my controller. Below is my view

<div class="form-group">
            <a class="btn btn-default" onclick="window.history.back()" href="javascript:"><i
                        class="fa fa-close"></i>
                Cancel</a>

            <?php if ($model->isNewRecord && in_array(Yii::$app->user->identity->access_level,['admin','manager'])) { ?>

                <?= Html::submitButton('Add' ,['class' => 'btn btn-success','name'=>'a']) ?>
                <?= Html::submitButton('Add & Send Quotation' ,['class' => 'btn btn-info','name'=>'s']) ?>
            <?php } elseif (!$model->isNewRecord && in_array(Yii::$app->user->identity->access_level,['admin'])) {?>
                <?= Html::submitButton('Update', ['class' =>'btn btn-warning']) ?>
            <?php }elseif (!$model->isNewRecord && in_array(Yii::$app->user->identity->access_level,['editor'])){?>

                <?php
                if ($model->reqStatus==\common\models\Accountant::$request_status_titles[0]
                    ||$model->reqStatus==\common\models\Accountant::$request_status_titles[2]
                )
                {

                    echo Html::submitButton('Verified', ['class' =>'btn btn-success','name'=>'v']);
                    echo Html::submitButton('Not-Verified', ['class' =>'btn btn-danger','name'=>'n']);
                }
                ?>

            <?php } elseif (!$model->isNewRecord && in_array(Yii::$app->user->identity->access_level,['manager'])){?>
                <?php
                if ($model->reqStatus==\common\models\Accountant::$request_status_titles[3])
                {
                    echo Html::submitButton('Signed',['class' =>'btn btn-primary','name'=>'g']);
                }
                elseif ($model->reqStatus==\common\models\Accountant::$request_status_titles[4])
                {
                    echo Html::submitButton('Follow-Up',['class' =>'btn btn-primary','name'=>'r']);
                }
                else{
                    echo Html::submitButton('Send Quotation',['class' =>'btn btn-primary','name'=>'q']);
                }
                ?>
            <?php } ?>
        </div>

In above code there are multiple submit button each with name. In my controller I want to get the name of Html::submitButton('Verified', ['class' =>'btn btn-success','name'=>'v']); this and echo Html::submitButton('Not-Verified', ['class' =>'btn btn-danger','name'=>'n']); this button.

 if (isset($_POST['v'])) //verified
        {
            $model->load(Yii::$app->request->post());
            $model->reqStatus = Accountant::$request_status_titles[1];
            $model->callVerified = 1;
            $model->clientRef = 'CL-'.uniqid();
            if($model->save())
            {

                return $this->redirect(['view', 'id' => $model->id]);
            }
            return $this->render('update', [
                'model' => $model,
            ]);

        }
        elseif (isset($_POST['n']))//not-verified
        {
            
            $model->load(Yii::$app->request->post());
            $model->reqStatus = Accountant::$request_status_titles[2];

            if($model->save())
            {

                return $this->redirect(['view', 'id' => $model->id]);
            }
            return $this->render('update', [
                'model' => $model,
            ]);
        }
        else
        {
            if ($model->load(Yii::$app->request->post()) && $model->save()) {
                return $this->redirect(['view', 'id' => $model->id]);
            }
            return $this->render('update', [
                'model' => $model,
            ]);
        }

When I execute it. I am able to see the verified and not verified button. But when I click on any of them, it doesn't go to the if (isset($_POST['v'])) or esleif (isset($_POST['n']))part but hits the else part. This same thing I am doing at another controller which is working perfectly fine.

Update 1

As per scaisEdge comment I have tried to var_dump($_POST) and below is the result

array(2) {
    ["_csrf-backend"]=> string(88) "xS7Ctb2C677HKQJjoBL2SPznK3IHVGUDSn7MGhMs9SigHov36PSI7J9OewzkSJ0AzpBYN0A_KTIlEIZfd1qiYQ=="
    ["Bookkeeping"]=> array(43) {
        ["name"]=> string(5) "Laeeq"
        ["conNumb"]=> string(11) "03464394600"
        ["email"]=> string(15) "laeeq@gmail.com"
        ["location"]=> string(12) "Amber Valley"
        ["reqConcerns"]=> string(22) "UK_limited_partnership"
        ["workStart"]=> string(21) "PROJECT_START_2_MONTH"
        ["businessName"]=> string(27) "Retail_consumer_merchandise" 
        ["reqPurpose"]=> string(40) "Select between several bookkeeper quotes" 
        ["bookAssign"]=> string(16) "One_time_project"
        ["bookkRequire"]=> string(32) "Only local bookkeepers can quote"
        ["bookWork"]=> string(15) "Annual accounts"
        ["companyName"]=> string(22) "NONPROFIT_ORGANIZATION"
        ["turnOver"]=> string(17) "£300,000-600,000"
        ["info"]=> string(7) "Testing"
        ["remarks"]=> string(15) "Client Verified"
        ["annualConf"]=> string(0) ""
        ["personalTaxRetHrmc"]=> string(0) ""
        ["mPaySlip"]=> string(0) ""
        ["mRtiPenRet"]=> string(0) ""
        ["annualAcc"]=> string(0) ""
        ["annualCorpTaxRet"]=> string(0) ""
        ["annualCorpTaxRetHrmc"]=> string(0) ""
        ["qVatRet"]=> string(0) ""
        ["qBookMtd"]=> string(0) ""
        ["insuranceHmrc"]=> string(0) ""
        ["corpTaxPlan"]=> string(0) ""
        ["persTaxPlan"]=> string(0) ""
        ["businessAdvice"]=> string(0) ""
        ["fedTax"]=> string(0) ""
        ["provTax"]=> string(0) ""
        ["invPerM"]=> string(0) ""
        ["invPerMPrice"]=> string(0) ""
        ["add1"]=> string(0) ""
        ["add2"]=> string(0) ""
        ["add3"]=> string(0) ""
        ["add4"]=> string(0) ""
        ["add5"]=> string(0) ""
        ["add6"]=> string(0) ""
        ["add7"]=> string(0) ""
        ["add8"]=> string(0) ""
        ["add9"]=> string(0) ""
        ["add10"]=> string(0) ""
        ["remarks2"]=> string(0) "" 
    }
}

There is no v string in the above result.

I must be missing something that I don't know.

Any help would be highly appreciated.

Michal Hynčica
  • 5,038
  • 1
  • 12
  • 24
Moeez
  • 494
  • 9
  • 55
  • 147
  • try check the real content of $_POST .. using var_dump($_POST) in controller – ScaisEdge Apr 06 '21 at 06:25
  • @scaisEdge you can check my `update 1` – Moeez Apr 06 '21 at 06:32
  • @scaisEdge the string `v` is not there – Moeez Apr 06 '21 at 06:43
  • but you have the name `n` .. ? .if you have just one of the two this is correct for your code .. an item can have just one name .. – ScaisEdge Apr 06 '21 at 07:13
  • @scaisEdge If I click on the `verified` button then the name passed should be `v` and if I click on the `not verified` button then the name passed should be `n`. In both cases, it's not happening. – Moeez Apr 06 '21 at 07:17
  • @scaisEdge this same mechanism is working on other forms and controllers but not this one :| – Moeez Apr 06 '21 at 07:22
  • in your var_dump you have not an entry for the button name this mean that the code in your form don't contain an elem with name v or n.. could be your rendering in form is not as you expected ... – ScaisEdge Apr 06 '21 at 07:25
  • @scaisEdge Then what should I do to check the form ? – Moeez Apr 06 '21 at 07:26
  • @scaisEdge The `Add` and `Add & Send quotation` buttons are working perfectly fine as both of the buttons are rendering the `Create Controller` but all the other buttons are rendered at `Update Controller` – Moeez Apr 06 '21 at 07:34

5 Answers5

0
<?php $form = ActiveForm::begin() ?>

Are you using this, before starting your form and ending it?

janavi
  • 26
  • 3
0

Your code is very difficult to read so you need to refactor it for simplicity. If I understand correctly, you have some conditions and you want different submit buttons to appear.

The first thing is none of the conditions require user input, so you can simply do the logic in the model.

For example

$model->isNewRecord && in_array(Yii::$app->user->identity->access_level,['admin','manager']

Does not require user input, so you can handle this in the model side.

Lionel Yeo
  • 383
  • 4
  • 13
0

I think that @Lionel Yeo is right. Your code is too complicated. It should be separated in many views, controller actions. Another option is that you can use Yii generated html links with GET parameters. If I read your code right..

If you want to see the incoming POST data.. - Just print Yii::$app->request->post() in the controller.

koredalin
  • 446
  • 3
  • 11
0

I had the same problem, it seemed to be causing it because I was using this

'enableClientScript' => false,

in ActiveForm::begin()

StVakis
  • 151
  • 2
  • 1
-1

the main problem is that you do not send a value back to the name.

Check out this answer:

Send value of submit button when form gets posted

in short your html markup should look like this:

<button type="submit" name="submit" value="Signed">Signed</button>
<button type="submit" name="submit" value="Follow-Up">Follow-Up</button>

In Yii2 view:

Html::submitButton('Signed',['class' =>'btn btn-primary','name' => 'submit', 'value' =>'Signed'])

In Yii2 controller:

if(Yii::$app->request->post('submit') === 'Signed') {
  • I have to submit it twice :| – Moeez Apr 06 '21 at 09:43
  • Hi @Moeez, you are right my answer was not good. In your case there should be an empty string value in $_POST. Maybe you could add a little bit more details to your question, at least the form start, end tag. Maybe you could check browser developer tool for the post information. (chrome - F12 - network tab - push submit button - select your request - headers - form data) – Papp Péter Apr 07 '21 at 04:50