1

How to save custom frontend form to database in Magento2.2.5?

Any help would be appreciated

asked Feb 21, 2019 at 7:55
4
  • 1
    You have created a frontend form or need the whole code including form creation? Commented Feb 21, 2019 at 7:57
  • I have created my form but I am not able to submit my values to db. Commented Feb 21, 2019 at 7:59
  • see my answer i have used this already in my form and its working for me. Commented Feb 21, 2019 at 8:03
  • Okay i will check it.. and let you know Commented Feb 21, 2019 at 8:03

1 Answer 1

0

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
1
  • can you share me your full code. Commented Feb 21, 2019 at 9:16

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.