{"id":1331,"date":"2020-11-04T09:59:30","date_gmt":"2020-11-04T09:59:30","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2020\/11\/04\/save-form-data-to-custom-table-in-magento-2\/"},"modified":"2025-05-21T17:46:18","modified_gmt":"2025-05-21T12:16:18","slug":"save-form-data-to-custom-table-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/save-form-data-to-custom-table-in-magento-2\/","title":{"rendered":"How to Save Form Data to the Custom Table in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Today\u2019s tutorial explains how you can&nbsp;save form data to the custom table in Magento 2.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When you fill out details in a form and click on a Submit button, the data has to be stored in a table. To do that, you will need to create&nbsp;<strong>Submit.php<\/strong>&nbsp;file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For instance, if you want to collect name, email addresses and telephone number of customers for sending newsletters, the data should be stored in a database. If the data is not stored in the database, we simply cannot access and send newsletters.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Learn the programmatic method as shown below:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Save Form Data to the Custom Table in Magento 2:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Create a file&nbsp;<strong><em><strong><em>app\\code\\Extension\\Controller\\Index\\Submit.php<\/em><\/strong><\/em><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace Meetanshi\\Extension\\Controller\\Index;\n\nuse Magento\\Framework\\App\\Action\\Context;\nuse Magento\\Framework\\View\\Result\\PageFactory;\nuse Meetanshi\\Extension\\Model\\ExtensionFactory;\nuse Magento\\Framework\\Controller\\ResultFactory;\nuse Magento\\Framework\\App\\Action\\Action;\n\nclass Submit extends Action\n{\n    protected $resultPageFactory;\n    protected $extensionFactory;\n\n    public function __construct(\n        Context $context,\n        PageFactory $resultPageFactory,\n        ExtensionFactory $extensionFactory\n    )\n    {\n        $this->resultPageFactory = $resultPageFactory;\n        $this->extensionFactory = $extensionFactory;\n        parent::__construct($context);\n    }\n\n    public function execute()\n    {\n        try {\n            $data = (array)$this->getRequest()->getPost();\n            if ($data) {\n                $model = $this->extensionFactory->create();\n                $model->setData($data)->save();\n                $this->messageManager->addSuccessMessage(__(\"Data Saved Successfully.\"));\n            }\n        } catch (\\Exception $e) {\n            $this->messageManager->addErrorMessage($e, __(\"We can\\'t submit your request, Please try again.\"));\n        }\n        $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);\n        $resultRedirect->setUrl($this->_redirect->getRefererUrl());\n        return $resultRedirect;\n\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Implement the code and you will be able to save data in the database. As we have written the message \u201cData Saved Successfully\u201d to display while submitting the data, the same message will appear when the data is being saved.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2020\/07\/How-to-Create-Custom-Form-in-Magento-2-Data-Save-1024x483.jpg\" alt=\"How to Save Form Data to the Custom Table in Magento 2\" class=\"wp-image-11177\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it. Also to prevent yourself from multiple form submission and storing same data of same user, once clicked on submit button&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/disable-submit-button-on-form-submission-in-magento-2\/\">disable submit button on form submission.<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Do consider sharing this blog post with the beginners who are willing to learn Magento extension development.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/meetanshi.com\/blog\/create-model-file-to-perform-crud-operation-in-magento-2\/\">How to Create Model File to Perform Crud Operation in Magento 2<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/meetanshi.com\/blog\/display-table-data-magento-2\/\">How to Display Data in Table Structue in Magento 2<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Today\u2019s tutorial explains how you can&nbsp;save form data to the custom table in Magento 2. When you fill out details in a form and click&#8230;<\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-1331","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1331","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=1331"}],"version-history":[{"count":5,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1331\/revisions"}],"predecessor-version":[{"id":13944,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1331\/revisions\/13944"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1331"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1331"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1331"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}