Read: https://magento.stackexchange.com/questions/222423/how-to-create-a-form-and-save-that-data-in-database-in-magento-2/222427
Create a directory Advcha/MyForm in your magento root project/app/code
Then create a new file registration.php:
|
1 2 3 4 5 6 |
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Advcha_MyForm', __DIR__ ); |
Then in etc/module.xml:
|
1 2 3 4 5 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Advcha_MyForm" setup_version="1.0.0"> </module> </config> |
Then do this to make sure it’s okay.
|
1 |
php bin/magento module:enable Advcha_MyForm && php bin/magento setup:upgrade |
Create a frontend router in etc/frontend/routes.xml:
|
1 2 3 4 5 6 7 8 |
<?xml version="1.0" encoding="UTF-8"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd"> <router id="standard"> <route id="myform" frontName="myform"> <module name="Advcha_MyForm"/> </route> </router> </config> |
I want the url to show the form is /myform/myform/index
So create a dir and file in Controller/MyForm/Index.php. But I need to create a layout first in view/frontend/layout/myform_myform_index.xml:
|
1 2 3 4 5 6 7 8 9 10 11 |
<?xml version="1.0"?> <page layout="2columns-left" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <head> <title>Advcha MyForm</title> </head> <body> <referenceContainer name="content"> <block class="Advcha\MyForm\Block\MyForm" name="myform.myform" template="Advcha_MyForm::myform.phtml"/> </referenceContainer> </body> </page> |
Then create a block in Block/MyForm.php:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
<?php namespace Advcha\MyForm\Block; class MyForm extends \Magento\Framework\View\Element\Template { /** * Construct * * @param \Magento\Framework\View\Element\Template\Context $context * @param array $data */ public function __construct( \Magento\Backend\Block\Template\Context $context, array $data = [] ) { parent::__construct($context, $data); } /** * Get form action URL for POST booking request * * @return string */ public function getFormAction() { // companymodule is given in routes.xml // controller_name is folder name inside controller folder // action is php file name inside above controller_name folder return '/myform/myform/index'; // here controller_name is myform, action is index } } |
Then create the template in view/frontend/templates/myform.phtml:
|
1 2 3 4 5 6 7 8 9 |
<h1>Advcha MyForm Page</h1> <form action="<?php echo $block->getFormAction() ?>" method="post"> <input name="firstname" type="text"> <input name="lastname" type="text"> <input name="phone" type="text"> <input name="bookingTime" type="date"> <input type="submit" value="Send informations"> </form> |
Then here is the controller in Controller/MyForm/Index.php:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
<?php namespace Advcha\MyForm\Controller\MyForm; use Magento\Framework\Controller\ResultFactory; class Index extends \Magento\Framework\App\Action\Action { /** * Booking action * * @return void */ public function execute() { // 1. POST request : Get booking data $post = (array) $this->getRequest()->getPost(); if (!empty($post)) { // Retrieve your form data $firstname = $post['firstname']; $lastname = $post['lastname']; $phone = $post['phone']; $bookingTime = $post['bookingTime']; // Doing-something with... // Display the succes form validation message $this->messageManager->addSuccessMessage('Send info done !'); // Redirect to your form page (or anywhere you want...) $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT); $resultRedirect->setUrl('/myform/myform/index'); return $resultRedirect; } // 2. GET request : Render the booking page $this->_view->loadLayout(); $this->_view->renderLayout(); } } |
DONE! PLS RUN
|
1 |
php bin/magento setup:upgrade && php bin/magento setup:static-content:deploy -f en_AU en_US --theme SCM/Minimal && php bin/magento c:f |
CHECK THE PAGE IN URL LIKE: http://scm.test/myform/myform/index
PLS TRY TO FILL THE FORM THEN PRESS THE SUBMIT BUTTON!