YII2如何使用pjax提交表单而不刷新页面

YII2 how to use pjax to submit a form without refresh the page

我用yii2创建了一个网站,试图用一个表单来提交一些数据。 有人能给我举个例子吗?告诉我如何在不重新加载页面的情况下使用 pjaxsubmit 表单。

这里有一个 Yii2 Pjax 提交表单的例子 link


views\site\form-submission.php:

<?php Pjax::begin(); ?>
    <?= Html::beginForm(['site/form-submission'], 'post', ['data-pjax' => '', 'class' => 'form-inline']); ?>
    <?= Html::input('text', 'string', Yii::$app->request->post('string'), ['class' => 'form-control']) ?>
    <?= Html::submitButton('Hash String', ['class' => 'btn btn-lg btn-primary', 'name' => 'hash-button']) ?>
    <?= Html::endForm() ?>
    <h3><?= $stringHash ?></h3>
<?php Pjax::end(); ?>

controllers\SiteController.php:

public function actionFormSubmission()
{
    $security = new Security();
    $string = Yii::$app->request->post('string');
    $stringHash = '';
    if (!is_null($string)) {
        $stringHash = $security->generatePasswordHash($string);
    }
    return $this->render('form-submission', [
        'stringHash' => $stringHash,
    ]);
}