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.
1 Answer 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
-
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.Clong– Clong2020年12月03日 08:32:06 +00:00Commented Dec 3, 2020 at 8:32
-
maybe you can use jQuery('#your-form').serialize() to get all values?Mario– Mario2020年12月03日 13:35:41 +00:00Commented Dec 3, 2020 at 13:35
-
another possibility is, to do your post with ajax, so there is no need to reload the page.Mario– Mario2020年12月03日 13:50:48 +00:00Commented Dec 3, 2020 at 13:50