We use cookies to make your experience better. To comply with the new e-Privacy directive, we need to ask for your consent to set the cookies. Learn more.
Method to Save Form Data to the Custom Table in Magento 2
In this tutorial, we will learn how to save form data in a custom table in Magento 2.
when you fill a form you need to send this form so you will click on submit button. By clicking on submit button, the data has to be stored in any table.
To do this process you need to create a submit.php file.
Create a file:
app\code\Module\Controller\Index\Submit.php
Code:
<?php
namespace TOH\Module\Controller\Index;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use Meetanshi\Extension\Model\ExtensionFactory;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\App\Action\Action;
class Submit extends Action
{
protected $resultPageFactory;
protected $extensionFactory;
public function __construct(
Context $context,
PageFactory $resultPageFactory,
ExtensionFactory $extensionFactory
)
{
$this->resultPageFactory = $resultPageFactory;
$this->extensionFactory = $extensionFactory;
parent::__construct($context);
}
public function execute()
{
try {
$data = (array)$this->getRequest()->getPost();
if ($data) {
$model = $this->extensionFactory->create();
$model->setData($data)->save();
$this->messageManager->addSuccessMessage(__("Data Saved Successfully."));
}
} catch (\Exception $e) {
$this->messageManager->addErrorMessage($e, __("We can\'t submit your request, Please try again."));
}
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
$resultRedirect->setUrl($this->_redirect->getRefererUrl());
return $resultRedirect;
}
}
Copy this code and paste it into your submit.php file, Now you can save your data in the database. when you fill in details in the form and click on submit button data will be saved. A message will show on the form page " Data Saved Successfully"
Screenshot
if you need any kind of help you can visit Magento Digital Agency
Related Products
Recent post
new admin user
magent 2.4
add new admin user using front-end
add new admin user using command line
admin user role
user role in magneto 2.4
magento 2.4
custom attribute
related products
product page
category
sub-category
how to add simple products
simple products
products in magento2
how to add simple products in magento2
Magento
store url
Full Page Cache
Translate CMS Pages
configurable product
how to add configurable product in magento2
magento2
magento2 products
eCommerce
Product Attribute Set
favicon
Import Products
Rss Feeds
how to configure rss feeds in magento 2
configuration of rss feeds
magento 2 structure
magento directory structure
module structure
magento design structure
theme structure of magento 2
e-commerce
magento2 agency
product
security
magento 2
attribute
CMS
block
admin-user
admin-role
product-attribute
page-cache
customize url
store
base url
CMS pages
basic structure
theme structure
design structure
root folder
migration
upgraded
speed up
improvements
seperate database
magento 1
shopify
new admin
create admin
elasticsearch
catalog search
simpleproduct
css
javascript
merge css
merge javascript
enable css
enable javascript
newsletter
php 8
compatibility
Multiple Stores
remarketing
boost sales
upsell-products
cross-sell products
payment method
check method
money order method
stock
product stock
configuration
Secure your Admin
VAT
tax classes
email logo
shipping policy
create new API
configure free shipping method
delete order in magento 2
features
magento store scope
cdn
cms-page
ux
uxdesign
magento-ux
ui-vs-ux
uidesign
user experience
product-list
uxscope
promote
marketing
SEO
Configure Email
Order Confirmation
redirect buyer
header
footer
layered-navigation
magento 2 store in multiple languages
inbox
message
Cancel order
edit order
magento and microsoft dynamics
integration
magento-theme
magento-layout
layout
magento-commerce
remove order
magento-store
magento-website
switch to Magento
Welcome message
Influence of Marketing
stock message
contact us form
contact email
admin login captcha
single store mode
single store
store view
magento installation
flat rate
shipping method
contact form
recaptcha
marketing plan
CSP
XSS
adding store view
magneto 2
B2B
B2C
D2C
Magento Commerce
Magento Commerce Cloud
guidepost
Small Business
Country Options
Magento Open Source
Product Search
magento-2-tutorial
rewrites
ecommerce agency
schedule design
customer account
magento hosting
hosting
magento configuration
Insert Images
magento developer
sales email
email configuration
Advanced Inventory
Warehouse
copyright
magneto copyright
copyright notice
update copyright
magento email store
store email address
update email address
Locale Option
programmatically
module
magento module
CMS Static Block
controller in magento2
magento controller
Override php file
plugin
plugin in magento2
helper function
magenot 2
call helper in phtml file
Backup Store
Magento Admin Panel
admin menu
model in magento
invoice in magento2
create invoice
Dependency Injection
di.xml
cronjob
cronjob in magento2
Api
events
event in magento2
Change
Default Icon
Menu Items
Backend
widget in magento 2
widget
custom widget
Add Store Demo
Api in magento 2
Run Compile Commad
custom tab
Mass Actions in magento 2
override classes
preference
helper in magento2
featured products
custom helper
checkout in magento 2
checkout page
customer collection
product collection
wishlist products collection
order collection
review in magento 2
rating in magento 2
review collection
rating collection
recent product collection
ui form
form in magento 2
magento 2 configuration
magento 2 fields
delete action in magento 2
edit action in magento 2
disable payment method
custom discount
virtual type
log
custom log
log file
eav attribute
installschema
database
product schema
production mode
indexer
reindexer
reindex magento 2
indexer magento 2
magento2 installation
email customization
custom email
email in magento 2
indexing
cart rules
catalog price rule
magento 2 catalog
currency in magento 2
command
console command
command in magento 2
store information in magento 2
extend layout
homepage
url in magento 2
error
google shopping feed
feed
magento2 feed
reindex magento 2 programatically
custom indexer
custom indexer for magneto 2
Magento 2 UX
Benefits of Magento 2 UX
magento 2 edit order
cancle order in magento 2
remove order in magento 2
update order in magento 2
magento 2 free shipping rule