I want to get post data in phtml . In case form data not save then show error and already fill form .

 <form class="trail-register-form" 
       action="<?php echo htmlspecialchars($this->getUrl().'xxxxxxx/post/register/'); ?>" 

 <input type="text" name="xxxxx_vehicle_name" placeholder="Vehicle Name" required>


$model->setData('xxxx_name', $post['xxxx_name']);
$id =$model->save() 

You can use Registry


You can save data by using

$this->registry->register('your_variable', $this->getRequest()->getParams()).

You can fetch data by using


In this case, you can use DataPersistor Magento\Framework\App\Request\DataPersistorInterface .

This class can save request data and share that to another url. DataPersistor is just like a session variable.

First, you have to save request data to a data persistor

using to a dataPersistor variable.

 $this->dataPersistor->set('my_request', $this->getRequest()->getParams());

Here the variable name is my_request.

Now, at your form block using Magento\Framework\App\Request\DataPersistorInterface you can fetch that variable value


ALso, after get that variable value, you have to clear that variable from dataPersistor using $this->dataPersistor->clear('my_request')

  1. Update




  2. controller.php

    $resultRedirect = $this->resultRedirectFactory->create();
    if ($this->getRequest()->isPost()) {
        $xxxx_name = $this->getRequest()->getPost('xxxxx_vehicle_name'); 
        if(!empty($xxxx_name)) {
                $model->load('id_here'); //you load your model first. 
                $model->setData('xxxx_name', $xxxx_name);
            }catch (\Exception $e) {
        }else {
            $message = __('Please fill in the field');
            return $resultRedirect;
    }else {
        return $resultRedirect;    
    // don't forget to inject dependencies of "messageManager", "ResultFactory", "logger"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.