How to get Submit Button Values in Controller/Action Yii2

时间:2015-04-23 05:32:57

标签: php yii yii2

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.

3 个答案:

答案 0 :(得分:10)

通过改变默认的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']) ?>

然后在控制器中

if (Yii::$app->request->post('submit')==='create_add') {
    // create add
} else {
    // submit
}

答案 1 :(得分:1)

试试这个:

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

答案 2 :(得分:0)

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']) ?>

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