php 如何在控制器/动作 Yii2 中获取提交按钮值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29814507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:36:18  来源:igfitidea点击:

How to get Submit Button Values in Controller/Action Yii2

phpyiiyii2

提问by J.K.A.

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

我是 Yii2 框架的新手。我想在我的控制器/操作中获取提交按钮名称/值。

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& $_REQUESTas well but still its not working.

我也尝试过$_POST&$_REQUEST但仍然无法正常工作。

Any help would be appreciated.

任何帮助,将不胜感激。

Thanks.

谢谢。

回答by J.K.A.

Resolved it myself by changing default Yii2 Buttons like this:

通过像这样更改默认的 Yii2 按钮自己解决了这个问题:

<?= 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
}

回答by user6243663

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"];
}

回答by friek108

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:

Mark 的回答适用于非 ajax 表单。如果要移动到 ajax 表单,则需要在按钮中添加值,如下所示:

<?= 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:

然后遵循与 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; 
});