0

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/'); ?>" 
       method="post" 
       enctype="multipart/form-data">

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

controller.php

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

You can use Registry

\Magento\Framework\Registry.

You can save data by using

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

You can fetch data by using

$this->registry->registry('your_variable').
1

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

 $this->dataPersistor->get('my_request');

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

0
  1. Update

    $this->getUrl().'xxxxxxx/post/register/

    to

    $block->getUrl("xxxxxxx/post/register/")

  2. controller.php

    $resultRedirect = $this->resultRedirectFactory->create();
    if ($this->getRequest()->isPost()) {
        $xxxx_name = $this->getRequest()->getPost('xxxxx_vehicle_name'); 
        if(!empty($xxxx_name)) {
            try{
                $model->load('id_here'); //you load your model first. 
                $model->setData('xxxx_name', $xxxx_name);
                $model->save();
            }catch (\Exception $e) {
                $this->logger->critical($e->getMessage());
            }
    
        }else {
            $message = __('Please fill in the field');
            $this->_messageManager->addError($message);
            $resultRedirect->setRefererUrl();
            return $resultRedirect;
        }   
    }else {
        $resultRedirect->setRefererUrl();
        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.