Q:

Submit multiple image upload forms on single button click

I have three forms in one view. Two forms have upload image (multipart) functionality and third is a regular form with text fields I want to upload all three forms on a single button click. can anyone help me with this?

A:

Try this code it will work to upload multiple images in different tables in a single form. 
public function actionCreate()
    {
        $db = Yii::$app->db;
        $transaction = $db->beginTransaction();
        $savedFlag = false;
        $model = new Company();
        $divisionModel = new Division();
        $prajects = new Prajects();

        if ($model->load(Yii::$app->request->post()) && $prajects ->load(Yii::$app->request->post()) && $divisionModel->load(Yii::$app->request->post())) {
            try{
                $model->company_logo = UploadedFile::getInstance($model, 'company_logo');
                    if ($model->company_logo) {                
                        $model->company_logo->saveAs('uploads/' . $model->company_logo->baseName . '.' . $model->company_logo->extension);
                    }
                    if($model->save()){
                         $savedFlag = true;
                        $divisionModel->division_logo = UploadedFile::getInstance($divisionModel, 'division_logo');
                        if ($divisionModel->division_logo) {                
                            $divisionModel->division_logo->saveAs('uploads/' . $divisionModel->division_logo->baseName . '.' . $divisionModel->division_logo->extension);
                        }
                        //$divisionModel->comapnay_id = $model->id;
                        if(!$divisionModel->save()){
                            $savedFlag = false;
                        }
                        if(!$prajects->save()){
                            $savedFlag = false;
                        }
                    }
                    if($savedFlag){
                        $transaction->commit();
                        return $this->redirect(['view', 'id' => $model->id]);
                    }else{
                        $transaction->rollBack();
                    }
            }catch (\Exception $e) {
                $transaction->rollBack();
                throw $e;
            }
        }

        return $this->render('create', [
            'model' => $model,
            'divisionModel' => $divisionModel,
            'prajects' => $prajects,
        ]);
    }

create.php file:
<?= $this->render('_form', [
        'model' => $model,
            'divisionModel' => $divisionModel,
            'prajects' => $prajects,
    ]) ?>

_form.php file:
<?php $form = ActiveForm::begin(); ?>

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

    <?= $form->field($model, 'company_logo')->fileInput() ?>
    <?= $form->field($divisionModel, 'division_name')->textInput(['maxlength' => true]) ?>

    <?= $form->field($divisionModel, 'division_logo')->fileInput() ?>
    <?= $form->field($prajects, 'name')->textInput(['maxlength' => true]) ?>

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

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

