1

Crud Operation for Frontend and Admin. How Frontend form data save into database in Magento2 and show in Admin grid?

Vrajesh Patel
2,0122 gold badges14 silver badges32 bronze badges
asked Feb 18, 2020 at 5:31

1 Answer 1

0

Try and look around this module : Github Magento 2 Simple CRUD Module

It contains exactly the same code for your needs, with simple form on frontend side which stores data on database and on admin using grid display list of those records and allows CRUD operations. Have a look.

I consider you already know how to create and register a module, so after that you'll need to create a frontend route and create a Controller, Block and layout and template file for that route and write your form code or whatever you need to input code in your template(.phtml) file.

Layout file (Vendor/Module/view/frontend/layout/:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd" layout="1column">
 <head>
 <css src="Invigorate_Inquiry::css/custom.css" />
 </head>
 <body>
 <referenceContainer name="content">
 <block class="Invigorate\Inquiry\Block\InquiryForm" name="inquiry_block" template="Invigorate_Inquiry::inquiry.phtml" cacheable="false"></block>
 <referenceBlock name="catalog.compare.sidebar" remove="true"/> 
 <referenceBlock name="wishlist_sidebar" remove="true" />
 <referenceBlock name="page.main.title" remove="true" />
 </referenceContainer>
 </body>
</page>

Template (.phtml) file (Vendor/Module/view/frontend/templates):

<?php
 $countryCollection = $block->getCountryCollection();
 $budget_limits = $helper->getNonSerializeData('inquiry/budget_group/budget_field_textarea');
 $budget_limits = explode(",",$budget_limits);
?>
<div class="inquiry-container">
 <h1><?php echo __('Fill up Inquiry form'); ?></h1>
 <center>
 <form id="inqury_form" class="form-container" name="inqury_form" method="POST" action="<?php echo $block->getFormAction() ?>">
 <input type="text" name="first_name" placeholder="Enter first name (Required)" data-validate="{required:true, 'letters-only':true, minlength:3}" /><br><br>
 <select name="country" data-validate="{required:true}">
 <option value="">Select Country (Required): </option>
 <?php foreach($countryCollection as $country){ 
 $country_id = $country['country_id'];
 $country_idd = $this->getCountryName($country_id);
 if($country_idd){ ?>
 <option value="<?php echo $country_idd; ?>"><?php echo $country_idd; ?></option>
 <?php }
 }?>
 </select><br><br>
 <input type="text" name="email_address" placeholder="E-mail Address (Required)" data-validate="{required:true}" /><br><br>
 <input type="text" name="phone_number" placeholder="Mobile number (Required)" data-validate="{required:true, 'no-whitespace':true, 'validate-number':true, minlength:8}" /><br><br>
 <select name="interested_area">
 <option value="">Interested In: </option>
 <?php foreach ($interested_area as $key => $data): ?>
 <option value="<?php echo $data['interested_fields']; ?>"><?php echo $data['interested_fields']; ?></option> 
 <?php endforeach; ?>
 </select><br><br>
 <select name="budget_area">
 <option value="">Budget Limits: </option>
 <?php foreach ($budget_limits as $key => $data): ?>
 <option value="<?php echo $data; ?>"><?php echo $data; ?></option> 
 <?php endforeach; ?>
 </select><br><br>
 <textarea name="description_msg" style="resize: none;" name="description_msg" placeholder="Enter your message here! (Required)" data-validate="{required:true, minlength:5, maxlength:100}"></textarea><br><br>
 <input type="submit" name="btn_submit_inquiry" value="Submit">
 <script type="text/x-magento-init">
 {
 "#inqury_form": {
 "validation": {}
 }
 }
 </script>
 </form>
</center>
</div>

Controller file which will get data and perform saving data to the database.

<?php
namespace Invigorate\Inquiry\Controller\Index;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\Controller\ResultFactory;
class Index extends Action {
 private $pageFactory;
 protected $_messageManager;
 protected $_inquiryRecordsDataFactory; //sdsdsd
 public function __construct(
 Context $context,
 PageFactory $pageFactory,
 \Magento\Framework\Message\ManagerInterface $messageManager,
 \Invigorate\Inquiry\Model\InquiryRecordsFactory $inquiryRecordsDataFactory //sdasd
 )
 {
 parent::__construct($context);
 $this->pageFactory = $pageFactory;
 $this->_messageManager = $messageManager;
 $this->_inquiryRecordsDataFactory = $inquiryRecordsDataFactory; //sdsdsd
 }
 public function execute()
 {
 $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
 $post = (array) $this->getRequest()->getPost();
 $this->_view->loadLayout();
 $this->_view->getPage()->getConfig()->getTitle()->set(__('Inquiry Page'));
 $this->_view->renderLayout();
 if(!empty($post))
 {
 try{
 $inquiry_first_name = $post['first_name'];
 $inquiry_country = $post['country'];
 $inquiry_email = $post['email_address'];
 $inquiry_phone = $post['phone_number'];
 $inquiry_interested_in = $post['interested_area'];
 $inquiry_budget_limit = $post['budget_area'];
 $inquiry_message = $post['description_msg'];
 $error = 0;
 if($inquiry_first_name == '')
 {
 $this->_messageManager->addError(__("First name is empty."));
 $error = 1;
 }
 if($inquiry_country == '')
 {
 $this->_messageManager->addError(__("Please select country."));
 $error = 1;
 }
 if($inquiry_email == '')
 {
 $this->_messageManager->addError(__("E-mail address is empty."));
 $error = 1;
 }
 if($inquiry_phone == '')
 {
 $this->_messageManager->addError(__("Phone number is empty."));
 $error = 1;
 }
 if($inquiry_interested_in == '')
 {
 $inquiry_interested_in = null;
 }
 if($inquiry_budget_limit == '')
 {
 $inquiry_budget_limit = null;
 }
 if($inquiry_message == '')
 {
 $this->_messageManager->addError(__("Please write a message."));
 $error = 1;
 }
 if($error == 0)
 {
 $model = $this->_inquiryRecordsDataFactory->create();
 $model->setData('first_name', $inquiry_first_name);
 $model->setData('country', $inquiry_country);
 $model->setData('email', $inquiry_email);
 $model->setData('phone', $inquiry_phone);
 $model->setData('interested_in', $inquiry_interested_in);
 $model->setData('budget_limit', $inquiry_budget_limit);
 $model->setData('inquiry_message', $inquiry_message);
 $model->save();
 $this->_messageManager->addSuccess(__("Thank you for the inquiry, our team will get back to you shortly."));
 $resultRedirect->setPath('inquiry');
 return $resultRedirect;
 }
 }catch(Exception $e)
 {
 echo $e->getMessage();
 }
 }
 }
}

To get complete module please check below link, as sharing all these files will make answer too lengthy. Link : Github Magento 2 Simple CRUD Module

answered Feb 18, 2020 at 6:43

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.