简体   繁体   中英

How to make controller remember ajax post data Yii2

I have multi modal windows, from one of it i want to send few $models to another window its like father heve few sons... and we can't save sons because we don't know father id and name..

JS

$("#modal-deposit").submit(function($form) {

    $form.preventDefault();
    $.ajax({
        type: "POST",
        url: "index.php?r=family/create", // сreatedeposit создает запись.
        data: $("#form-deposit").serialize(), // Сериализует the form's elements.
 return false;
});

Action

public function actionCreate()
    {
        $family_model = new family();
        $model_deposit = new FinanceIncomeExpenses();

        if ($family_model->load(Yii::$app->request->post())
            && $family_model->save())
        {
           if ($model_deposit->sum != null)// one of the required fields
            {
               $model_deposit->family_id = $family_model->id;
               $model_deposit->save();
            }


        } else {
            if(Yii::$app->getRequest()->isAjax){
                return $this->renderAjax('create', [
                    'family_model' => $family_model,
                    'model_deposit' => $model_deposit,
                ]);
            }else{
                return $this->render('create', [
                    'family_model' => $family_model,
                    'model_deposit' => $model_deposit,
                ]);
            }
        }
    }

for one you would need to change

url: "index.php?r=family/create",

to

url: "<?php echo \Yii::$app->getUrlManager()->createUrl('family/create') ?>",

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