【问题标题】:Pjax and form submit not woking in yii2Pjax和表单提交在yii2中没有工作
【发布时间】:2016-12-29 09:09:21
【问题描述】:

我正在开发一个注册页面,但该页面的某些部分应单独提交。我进行了一些研究,发现 yii2 中的 Pjax 和我在文档中发现的内容无法正常工作。

  1. 没有创建标签...只有一个包含 csrf 的隐藏元素。
  2. 提交后,所有页面都会重新加载。

查看:

<div class="row profileimg">
    <?php Pjax::begin(['timeout' => 40000, 'id' => 'myPjax',]); ?>
    <?= Html::beginForm(['couple-partner/saveprofileimg'], 'post', ['id' => 'CouplePartnerSumb', 'data-pjax' => true, 'class' => 'form-inline']);                          ?>

    <div class="col-lg-12" onmouseover="onmouseOverImg('FirstPartnerEditButton', 'FirstPartnerDelButton');" onmouseout="onmouseoutImg('FirstPartnerEditButton', 'FirstPartnerDelButton')" id="imgdiv" style="display:inline-block;position: relative;left : 35%;height: 110px;width: 150px;">
        <img src="<?= $models != NULL && sizeof($models) > 0 && $models[0]->USER_PROFILE_PIC != NULL ? $models[0]->USER_PROFILE_PIC : "" ?>" style="cursor: pointer;width:100%;height:100% " id="firstPartnerProfilePic" class="img-thumbnail ">
        <?= Html::fileInput('imageFile', '') ?>

        <?php echo Html::submitButton('Upload', ['class' => 'btn btn-primary', 'id' => 'submit_iddddd',]) ?>

    </div>
    <?php echo Html::endForm();Pjax::end();?>
</div>

控制器:

public function actionSaveprofileimg() {
    $model = new CouplePartner();

    if (Yii::$app->request->isAjax) {
        $model->imageFile = UploadedFile::getInstance($model, 'imageFile');
        if ($model->upload()) {
            // file is uploaded successfully
            return;
        }
    }

    return $this->renderAjax('index', ['model' => $model]);
}

我找不到Pjax无法正常工作的问题是什么以及为什么提交按钮在单击时会重新加载所有页面。

【问题讨论】:

  • 首先检查正常的发布条件,如 $model->load(Yii::$app->request->post()

标签: php yii2 pjax


【解决方案1】:

页面重新加载,因为控制器抛出 500 错误。 你应该改变 $model->upload() $model->imageFile->upload() 并尝试在控制器处理上传请求时发现更多问题

【讨论】:

  • 提交按钮...提交主表单...我不知道为什么。
  • 顺便说一句,我需要使用 yii 和 pjax 上传文件......这就是我想要实现的目标。
  • 没关系。只需添加更多表单属性:multipart/form-data 然后您就可以从控制器获取文件
猜你喜欢
  • 2015-11-14
  • 1970-01-01
  • 1970-01-01
  • 2013-02-09
  • 1970-01-01
  • 1970-01-01
  • 2013-10-28
  • 1970-01-01
  • 2017-03-26
相关资源
最近更新 更多