How to save custom frontend form to database in Magento2.2.5?
Any help would be appreciated
-
1You have created a frontend form or need the whole code including form creation?Amit Naraniwal– Amit Naraniwal2019年02月21日 07:57:12 +00:00Commented Feb 21, 2019 at 7:57
-
I have created my form but I am not able to submit my values to db.Amy– Amy2019年02月21日 07:59:06 +00:00Commented Feb 21, 2019 at 7:59
-
see my answer i have used this already in my form and its working for me.Amit Naraniwal– Amit Naraniwal2019年02月21日 08:03:05 +00:00Commented Feb 21, 2019 at 8:03
-
Okay i will check it.. and let you knowAmy– Amy2019年02月21日 08:03:47 +00:00Commented Feb 21, 2019 at 8:03
1 Answer 1
You can save the form data using the controller :
namespace VendorName\ModuleName\Controller\Customer;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
class Save extends \Magento\Framework\App\Action\Action
{
protected $resultPageFactory;
protected $session;
public function __construct(
\Magento\Framework\App\Action\Context $context,
PageFactory $resultPageFactory
)
{
parent::__construct($context);
$this->resultPageFactory = $resultPageFactory;
}
public function execute()
{
$resultRedirect = $this->resultRedirectFactory->create();
//$resultPage = $this->resultPageFactory->create();
//$resultPage->getConfig()->getTitle()->set(__('Helpdesk'));
//return $resultPage;
$data = $this->getRequest()->getPostValue();
$model = $this->_objectManager->create('VendorName\ModuleName\Model\ModelName');
$model->setData($data);
try {
$model->save();
$this->messageManager->addSuccess(__('The Data has been saved.'));
return $resultRedirect->setPath('*/*/');
} catch (\Magento\Framework\Exception\LocalizedException $e) {
$this->messageManager->addError($e->getMessage());
} catch (\RuntimeException $e) {
$this->messageManager->addError($e->getMessage());
} catch (\Exception $e) {
$this->messageManager->addException($e, __('Something went wrong while saving the Data.'));
}
}
}
answered Feb 21, 2019 at 8:02
Amit Naraniwal
1,3081 gold badge11 silver badges20 bronze badges
-
can you share me your full code.Amy– Amy2019年02月21日 09:16:16 +00:00Commented Feb 21, 2019 at 9:16
default