B
B
baetov2020-12-15 17:42:12
PHP
baetov, 2020-12-15 17:42:12

How to put data from the form into the session and then from the session into the form?

Hello, please advise.
So I have an article tab, when adding, a modal window opens

spoiler
5fd8c86c465ef908139589.png

for convenience, so as not to jump through the pages, I added a plus button to call group/create directly from here.
here is the button code from the article/_form form
<div class="col-md-1">
            <?= Html::a('<span class="fa fa-plus"></span>', [
                    '/group/create',
                    'caller' => '/article/create',
                 ], [
                        'role'=>'modal-remote',
                        'title'=> 'Добавить группу',
                        'class' => 'btn btn-default btn-block',
                ]);?>

        </div>

when clicked, it means that I open a group modal, after saving I get a window about successful saving
spoiler
5fd8ca3097b49684448475.png

here is the code from the group controller
if ($model->load($request->post()) && $model->save()) {
                   if (!$caller){
                       return [
                           'forceReload' => '#crud-datatable-pjax',
                           'size' => 'md',
                           'title' => "Создание новой группы",
                           'content' => '<span class="text-success">Группа добавлена</span>',
                           'footer' => Html::button('Закрыть',
                                   ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) .
                               Html::a('Создать ещё', ['create'],
                                   ['class' => 'btn btn-primary', 'role' => 'modal-remote'])


                       ];
                   }else{
                       return [
                           'forceReload' => '#crud-datatable-pjax',
                           'size' => 'md',
                           'title' => "Создание новой группы",
                           'content' => '<span class="text-success">Группа добавлена</span>',
                           'footer' => Html::a('Вернуться', [$caller],
                                   ['class' => 'btn btn-primary', 'role' => 'modal-remote'])


                       ];
                   }

when calling, I pass in the caller variable the action from where I called and return back to article/create , but I lose the data that I filled in before pressing the button when I return , I would like to save them to the session, tell me how to do this
$caller = $request->get('caller');

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shamanov, 2020-12-16
@SilenceOfWinter

Because sending by ajax, then you can simply not reload the page, js / jquery:

$('form.article').on('submit', function (event) {
   event.preventDefault();
   let form = $(this);
   $.post(form.attr('action'), form.serialize(), function (response) {...});
});

Regarding storage in the session: we convert the data into a string and save it using regular means , and then the reverse process$_SESSION['form'] = json_encode($_POST);
$_POST = json_decode($_SESSION['form'] ?? '{}', true);
.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question