2

I'm new in Yii2 Framework. I want to get Submit Button Name/Value in my Controller/Action.

Following is my code:

Form:

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

<?= $form->field($model, 'admin_document_key_id')->dropDownList(ArrayHelper::map(AdminDocumentKey::find()->all(), 'id', 'key_name'), ['prompt' => 'Select']) ?>

<?= $form->field($model, 'key_value')->textInput(['maxlength' => 255]) ?>    

<div class="form-group">
    <?php if (Yii::$app->controller->action->id == "create"): ?>
        <?= Html::submitButton('Create & Add New', ['class' => 'btn btn-primary']) ?>
    <?php endif; ?>

            <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary', 'value'=>'Create', 'name'=>'submit']) ?>
    <?= Html::a('Cancel', ['/admindocumentvalue'], ['class' => 'btn btn-warning']) ?>
    <?= Html::resetButton('Reset', ['class' => 'btn btn-info']) ?>
</div>

Controller/Action:

public function actionCreate()
 {
    $model = new AdminDocumentValue();

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

It showing following Output:

Array ( [_csrf] => V0FKc09GclQDbHI1BRVEIR8xOwcjLEFiBSsyGBcZGj4dMWc6JzItbA== [AdminDocumentValue] => Array ( [admin_document_key_id] => 1 [key_value] => Claims ) ) 

But not showing submit button name and value.

I tried $_POST & $_REQUEST as well but still its not working.

Any help would be appreciated.

Thanks.

5
  • Do you want button (HTML element) name and value or form values? Commented Apr 23, 2015 at 5:36
  • Form values including submit button values Commented Apr 23, 2015 at 5:38
  • Simply I just want to redirect user depending on button clicked Commented Apr 23, 2015 at 5:38
  • Submit button is needed only for submitting form, how it can have value? Commented Apr 23, 2015 at 5:39
  • Let us continue this discussion in chat. Commented Apr 23, 2015 at 5:39

3 Answers 3

11

Resolved it myself by changing default Yii2 Buttons like this:

<?= Html::submitButton('Create & Add New', ['class' => 'btn btn-primary', 'value'=>'create_add', 'name'=>'submit']) ?>
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary', 'value'=>'Create', 'name'=>'submit']) ?>

Then in controller

if (Yii::$app->request->post('submit')==='create_add') {
    // create add
} else {
    // submit
}
Sign up to request clarification or add additional context in comments.

3 Comments

How are you solving your controller logic whit this button-values?
If you are submitting a form via ajax, you need to follow similar logic to that in yii.activeForm.js
Just to add on, the name of each submit button needs to be different (e.g. submit-create-add and submit-create), as it leads to some weird behaviours of the button in some browsers. With buttons of the same name, I had to click the button twice to submit the form.
1

Try this:

if ($model->load(Yii::$app->request->post())) {
    $data=Yii::$app->request->post('Postdata');
    print_r(json_encode($data));
    print_r($data["post_id"]);
    $model->post_id =$data["post_id"];//$_POST["post_id"];
    $model->title =$data["title"];// $_POST["title"];
    $model->description =$data["description"];//$_POST["description"];
    $model->user_id =$data["user_id"];//$_POST["user_id"];
}

Comments

0

Mark's answer works for non-ajax forms. If you want to move to ajax forms, you need to do add values in the buttons like this:

<?= Html::submitButton('Create & Add New', ['class' => 'btn btn-primary', 'value'=>'Create & Add New', 'name'=>'submit']) ?>
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary', 'value'=>'Create', 'name'=>'submit']) ?>

And then follow a similar logic to that in yii.activeForm.js:

// send data to actionSave by ajax request.
$(document).on("beforeSubmit", "#my-form-id", function () {            
    var data = $(this).data('yiiActiveForm'),
    $button = data.submitObject,
    extData = '&' + data.settings.ajaxParam + '=' + $(this).attr('id');
    if ($button && $button.length && $button.attr('name')) {
      extData += '&' + $button.attr('name') + '=' + $button.attr('value');
    }
    $.post($(this).attr('action'), $(this).serialize() + extData
    ).done(function(result){
      // process      
    });
    return false; 
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.