如何使用ajax提交模态并将数据发送到另一个模态YII2

时间:2015-03-30 14:21:25

标签: jquery ajax yii2

我有模态窗口,我从中调用另一个模态。我想关闭第二个模态并将$ model发送到第一个模态(动作)...我使用此代码http://www.yiiframework.com/wiki/690/render-a-form-in-a-modal-popup-using-ajax/

查看

<?php $form = ActiveForm::begin([
 'id' => 'form-deposit',
 'action' => ' ',
 'options'=>[
    'onsubmit'=>'submitForm',
 ]
 ]);?>
 <?= $form->field($model, 'sum')->textInput() ?>

JS

function submitForm($form) {
    $.post(
        $form.attr("action"), // serialize Yii2 form
        $form.serialize()
    )
        .done(function(result) {
            //$form.parent().html(result.message);
            $('#modalreference-edit').modal('hide');
        })
        .fail(function() {
            console.log("server error");
            $form.replaceWith('<button class="newType">Fail</button>').fadeOut()
        });
    return false;
}    

控制器

public function actionCreatedeposit()
    {
        $model = new FinanceIncomeExpenses();
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            $model->refresh();
        }     
        else {
            if(Yii::$app->getRequest()->isAjax){
                return $this->renderAjax('deposit/create', [
                    'model' => $model,
                ]);
            }else{
                return $this->render('deposit/create', [
                    'model' => $model,
                ]);
            }
        }
    }

如果我禁用动作它会关闭两个模态窗口并且没有在db中写入任何内容。当我使用动作时,我得到了空白页。

0 个答案:

没有答案