0

I have a webpage with a form when I submit that page I go into a controller...he check the data sent $data = $this->getRequest()->getPostValue();

And if one of the data isn't what I am expecting, i'm redirecting on the same url page with an error message.

$this->messageManager->addError($errorMessage);
return $resultRedirect->setUrl($this->_redirect->getRefererUrl());

This works fine.

But doing this, i'm loosing all the datas registered previously by the user (cause it's like if I was opening the url for the first time obviously).

I'm looking for a way to keep that data in the html so may be there is a different kind of redirect possible ?

To resume : Is it possible from php server side to reload a url by keeping the input values.

Claims
  • 1,154
  • 9
  • 26

1 Answers1

1

Your controller could set the data to a session. The page with the form can check for data in that session and set the fields accordingly.

After the data was sent correctly, you can unset the session-data.

Here is an answer to see, how to set/get session-data with magento: how to set session variables

Mario
  • 478
  • 1
  • 6
  • 13
  • I'm affraid the form is too big and too split into many templates to make a solution like that one to work, especially when there is many js in the background. But indeed the workaround would work I guess. – Claims Dec 03 '20 at 08:32
  • maybe you can use jQuery('#your-form').serialize() to get all values? – Mario Dec 03 '20 at 13:35
  • another possibility is, to do your post with ajax, so there is no need to reload the page. – Mario Dec 03 '20 at 13:50