Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Yii2向我的表提交两次ajax插入数据_Php_Ajax_Yii2_Yii2 Advanced App - Fatal编程技术网

Php Yii2向我的表提交两次ajax插入数据

Php Yii2向我的表提交两次ajax插入数据,php,ajax,yii2,yii2-advanced-app,Php,Ajax,Yii2,Yii2 Advanced App,在Yii2上,在表格中插入两次我的表格,如果我点击两次我的提交按钮,我的表格将数据插入表格,就像我点击两次我的提交按钮一样;我正在使用ajax提交表单;我看到的表格是 <script type="text/javascript"> $(document).ready(function (e) { $("#upload-gallery").on('submit',(function(e) { $form = $(this);

在Yii2上,在表格中插入两次我的表格,如果我点击两次我的提交按钮,我的表格将数据插入表格,就像我点击两次我的提交按钮一样;我正在使用ajax提交表单;我看到的表格是

<script type="text/javascript">
    $(document).ready(function (e) {
        $("#upload-gallery").on('submit',(function(e) {
            $form = $(this);
            e.preventDefault();
            $.ajax({
                url: $form.attr('action'),
                type: "POST",
                data:  new FormData(this),
                contentType: false,
                cache: false,
                processData:false,
                success: function(data)
                {
                    document.getElementById("upload-gallery").reset();
                    $.pjax.reload({container: '#some_pjax_id', async: false});
                },
                    error: function(){}             
           });
        }));    
    });
</script>

<div class="post-gallery-form">

    <?php $form = ActiveForm::begin(['id' => 'upload-gallery', 'options' => ['enctype' => 'multipart/form-data']]) ?>

    <?= $form->field($model_PstGlr, 'PGalleryFile[]')->fileInput(['multiple' => true, 'accept' => 'image/*']) ?>

    <?= $form->field($model_PstGlr, 'post_text')->textarea(['rows' => 2]) ?>

    <?= $form->field($model_PstGlr, 'permission_id')->dropdownList($model_Permission->PermissionOn()) ?>

    <div class="form-group">
        <?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
    </div>

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

</div>
上载图库功能是在我的内容上上载图像

--


令人震惊的是,您可能需要在代码中进行一些更改。 例如,在控制器中创建以下函数,并从视图中删除代码

public function actionSave(){
//This part is removed from the actionView, so that the request only saves and does nothing else; 
//You can return it back if you test and see that it works
$model_PstGlr = new PostGallery;    $acc_PstGlr = new AxPostGallery;
   if ($model_PstGlr->load(Yii::$app->request->post()) )
   {
    $acc_PstGlr->CreateGallery($model_PstGlr, $chn);
   }
}
然后在视图上,修改表单初始化,如下所示 添加forwad斜杠的原因是为了防止apache将请求发送到以
“/”结尾的新URL

<?php $form = ActiveForm::begin(['action'=> 'id' => 'upload-gallery', 'options' => ['enctype' => 'multipart/form-data']]) ?>


我希望这一个现在有帮助

添加完整的代码,您没有从您的模型中添加
CreateGallery
方法我确实将CreateGallery函数添加到了我的代码中我没有发现任何可疑之处您确定您没有绑定提交事件两次吗?控制台选项卡中是否只显示一个ajax请求?以及添加两次的记录,用于
Gallery
PGallery
?对于PGallery和Gallery,它记录了两次PGallery和Gallery尝试不工作;我只单击了一次,但它是插入twice@Bynd尝试删除
$(document).ready(函数(e){
,并将其替换为
jQuery(函数($){
并让我知道它是否仍然提交了两次。是否有可能您正在加载两次主jquery文件,或者它也被另一个java脚本文件加载了?我不认为soOkay,让我看看是否可以找到替代方案。即使在我的测试服务器上,该场景实际上也在重复。
public function actionSave(){
//This part is removed from the actionView, so that the request only saves and does nothing else; 
//You can return it back if you test and see that it works
$model_PstGlr = new PostGallery;    $acc_PstGlr = new AxPostGallery;
   if ($model_PstGlr->load(Yii::$app->request->post()) )
   {
    $acc_PstGlr->CreateGallery($model_PstGlr, $chn);
   }
}
<?php $form = ActiveForm::begin(['action'=> 'id' => 'upload-gallery', 'options' => ['enctype' => 'multipart/form-data']]) ?>