Today I am going to explain How to Create Custom enquire Form in Magento 2.
Forms are a great way to gather customer information and insert into database or send email to the admin user
as per the requirement. With the help of the below code, you can Create Custom Form in Magento 2 Frontend.
URL making pattern : frontName/Controller folder/Controller file name. all in small letters
In my case URL should be : example.com/enquiry/index/index/
Step 1 .Create routes.xml file.
File: app/code/Myvendor/Mymodule/etc/frontend/routes.xml
<?xml version="1.0" ?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd"> <router id="standard"> <route id="enquiry" frontName="enquiry"> <module name="Myvendor_Mymodule"/> </route> </router> </config>
Note : id refer to the xml file name and frontName refer to URL
Step 2 .Create enquiry_index_index.xml file.
File :app/code/Myvendor/Mymodule/view/frontend/layout/enquiry_index_index.xml
<?xml version="1.0" ?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <body> <referenceContainer name="content"> <block name="index.index" class="Myvendor\Mymodule\Block\Enquiry" template="Myvendor_Mymodule::enquiry-form.phtml"/> </referenceContainer> </body> </page>
Note: This is a Block class : class=”Myvendor\Mymodule\Block\Enquiry”
Step 3. Create enquiry-form.phtml file.
File :app/code/Myvendor/Mymodule/view/frontend/templates/enquiry-form.phtml
<form> <input type="text" name="name" /> <input type="text" name="email" /> </form>
Step 4. Create Block Enquiry.php file.
File :app/code/Myvendor/Mymodule/Block/Enquiry.php
<?php declare(strict_types=1); namespace Myvendor\Mymodule\Block; class Enquiry extends \Magento\Framework\View\Element\Template { /** * Constructor * * @param \Magento\Framework\View\Element\Template\Context $context * @param array $data */ public function __construct( \Magento\Framework\View\Element\Template\Context $context, array $data = [] ) { parent::__construct($context, $data); } }
Step 5. Create Controller Index.php file.
File : app/code/Myvendor/Mymodule/Controller/Index/Index.php
<?php declare(strict_types=1); namespace Myvendor\Mymodule\Controller\Index; use Magento\Framework\App\Action\HttpGetActionInterface; use Magento\Framework\Controller\ResultInterface; use Magento\Framework\View\Result\PageFactory; class Index implements HttpGetActionInterface { /** * @var PageFactory */ protected $resultPageFactory; /** * Constructor * * @param PageFactory $resultPageFactory */ public function __construct(PageFactory $resultPageFactory) { $this->resultPageFactory = $resultPageFactory; } /** * Execute view action * * @return ResultInterface */ public function execute() { return $this->resultPageFactory->create(); } }
Step 6 .Create Save Controller Save.php file.
File :app/code/Myvendor/Mymodule/Controller/Index/Save.php<?php declare(strict_types=1); namespace Myvendor\Mymodule\Controller\Index; use Magento\Framework\App\Action\HttpGetActionInterface; use Magento\Framework\Controller\ResultInterface; use Magento\Framework\View\Result\PageFactory; use Magento\Framework\App\ActionInterface; use Magento\Framework\App\ResponseInterface; use Magento\Store\Model\StoreManagerInterface; class Save implements ActionInterface { protected $resultPageFactory; protected $request; protected $resultFactory; protected $messageManager; /** * Constructor * * @param PageFactory $resultPageFactory */ public function __construct( \Magento\Framework\App\RequestInterface $request, \Magento\Framework\Controller\ResultFactory $resultFactory, \Magento\Framework\Message\ManagerInterface $messageManager, PageFactory $resultPageFactory ) { $this->resultPageFactory = $resultPageFactory; $this->request = $request; $this->resultFactory = $resultFactory; $this->messageManager = $messageManager; } /** * Execute view action * * @return ResultInterface */ public function execute() { $post = $this->request->getPostValue(); $redirect = $this->resultFactory->create(\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT); $redirect->setPath('sure-new'); if(!$post) { $this->messageManager->addError(__("There is no data available")); return $redirect; } } }
Related Post on Add Re-Captcha In Magento 2