简体   繁体   中英

Yii2 submit ajax insert twice data to my table

on Yii2 Advanced My form insert twice in table, if I click twice my submit button my form insert data to my table twice it's like I clicked two time on my submit button; I'm using ajax for submit my form; my form in view is

<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>

and my controller is

public function actionView($chn)
{
    $model_Permission = new Permission;

    $model_PstGlr = new PostGallery;    $acc_PstGlr = new AxPostGallery;

    if ($model_PstGlr->load(Yii::$app->request->post()) )
    {
        $acc_PstGlr->CreateGallery($model_PstGlr, $chn);
    }

    return $this->render('view', [
        'model' => $this->findModel($chn),
        'model_Permission' => $model_Permission,
        'model_PstGlr' => $model_PstGlr,

    ]);
}

The upload gallery function is to upload images on my contents

--

public function CreateGallery ($MPGALLERY, $CHNID)
    {
        $PSTID = Yii::$app->params['PSTID'];
        $USRID = Yii::$app->user->id;

        $MPGALLERY->post_id = $PSTID;
        $MPGALLERY->user_id = $USRID;
        $MPGALLERY->channel_id = $CHNID;

        $ly_lgIMGFolder = 'upload/gal/'. $CHNID . '/' . $PSTID . '/' .date('YzHis');

        $GLRID = Yii::$app->params['GLRID'];

        $MPGALLERY->PGalleryFile = UploadedFile::getInstances($MPGALLERY, 'PGalleryFile');

        if( $MPGALLERY->save() )
        {
            mkdir($ly_lgIMGFolder, 0777, true);

            foreach ($MPGALLERY->PGalleryFile as $GImage)
            {
                $MGALLERY = new Gallery;

                $MGALLERY->post_id = $PSTID;

                $GImage->saveAs($ly_lgIMGFolder ."/". $GLRID . '.' . $GImage->extension);

                $MGALLERY->gallery_lgimage = $ly_lgIMGFolder ."/". $GLRID . '.' . $GImage->extension;

                $MGALLERY->save(false);
            }
        }
    }

Appatently, there are a few changes that you may need to make in your code. For example, create the following function in your controller, and remove the code from your view.

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);
   }
}

Then on the view, modify your form initialization as follows The reason for adding the forwad slash is to prevent apache from sending the request to a new URL that ends with the '/' ;

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

I hope this one now helps;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM