Javascript 从yii2 ActiveForm保存数据后无法显示成功消息(在模式中)

Javascript 从yii2 ActiveForm保存数据后无法显示成功消息(在模式中),javascript,modal-dialog,yii2,active-form,Javascript,Modal Dialog,Yii2,Active Form,在主页上,我有一个按钮,用activeform调用引导模式。提交时,此表单在my db中添加新行。但无论是在这个模式中还是在主页上,我都没有看到成功的信息。相反,我进入了mysite.ru/category/create页面,页面上有白色屏幕和对话框消息文本,对话框内容如下。。。。如何不重定向其他页面上的用户?只需关闭主页上的模式和/或重定向,并在此处显示有关成功添加行的flashmessage views/site/index.php中的“我的”按钮: <?= Html::button(

在主页上,我有一个按钮,用activeform调用引导模式。提交时,此表单在my db中添加新行。但无论是在这个模式中还是在主页上,我都没有看到成功的信息。相反,我进入了mysite.ru/category/create页面,页面上有白色屏幕和对话框消息文本,对话框内容如下。。。。如何不重定向其他页面上的用户?只需关闭主页上的模式和/或重定向,并在此处显示有关成功添加行的flashmessage

views/site/index.php中的“我的”按钮:

<?= Html::button(
        'create',
        ['value' => Url::to(['category/create']),
            'id' => 'modalButton'

        ]) ?>


    <?php
    Modal::begin([
            'id' => 'modal'
        ]);

    echo "<div id='modalContent'></div>";

    Modal::end();
    ?>
类别控制器具有

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

            if ($model->load(Yii::$app->request->post()) && Yii::$app->request->isAjax) {
                $model->refresh();
                Yii::$app->response->format = 'json';
                return ActiveForm::validate($model);
            } elseif ($model->load(Yii::$app->request->post()) && $model->save()) {
                Yii::$app->session->setFlash('contactFormSubmitted');
                //$model->refresh();
                $this->refresh();
                Dialog::begin([
                    'clientOptions' => [
                        'modal' => true,
                    ],
                ]);

                echo 'Dialog contents here...';

                Dialog::end();
                //$this->redirect('category/create');

            } else {
                return $this->renderAjax('create', [
                    'model' => $model,
                ]);
            }
views/category/create.php:

<?php

use yii\helpers\Html;


/* @var $this yii\web\View */
/* @var $model app\models\Category */

$this->title = 'Create Category';
$this->params['breadcrumbs'][] = ['label' => 'Categories', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;

?>
<div class="category-create">

    <h1><?= Html::encode($this->title) ?></h1>

    <?= $this->render('_form', [
        'model' => $model,
    ]) ?>

</div>
和视图/category/_form.php

<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\jui\Dialog;

/* @var $this yii\web\View */
/* @var $model app\models\Category */
/* @var $form yii\widgets\ActiveForm */
?>

<?php
$this->registerJsFile(Yii::getAlias('/scripts/main.js'));

?>

    <?php if (Yii::$app->session->hasFlash('contactFormSubmitted')): ?>

    <div class="alert alert-success">
        Thank you for contacting us. We will respond to you as soon as possible.
    </div>

    <?php endif; ?>



<div class="category-form">

    <?php $form = ActiveForm::begin([
                'id' => $model->formName(),
                //'enableAjaxValidation'=>true,
                'validateOnSubmit'=>true,
                //'action' => '/site/index'
            ]); ?>

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

    <?= $form->field($model, 'description')->textarea(['rows' => 6]) ?>

    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>
脚本/main.js:

// listen click, open modal and .load content
$('#modalButton').click(function (){
    $('#modal').modal('show')
        .find('#modalContent')
        .load($(this).attr('value'));
});

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

您必须防止表单被提交,并使用ajax

            $(your_form)
            .on('beforeSubmit', function(event){ 
                event.preventDefault();
                submitForm($(your_form));

                return false;
            })
            .on('submit', function(event){
                event.preventDefault();
            });
            $(your_form)
            .on('beforeSubmit', function(event){ 
                event.preventDefault();
                submitForm($(your_form));

                return false;
            })
            .on('submit', function(event){
                event.preventDefault();
            });