1

I am making form in front end and want to save data in database in Magento 2, i have used below code but when i am using link page shows Blank

My/Module/Block/contact.php

<?php

namespace My\Module\Block;

/**
 * Test content block
*/
class Contact extends \Magento\Framework\View\Element\Template
{
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context
    ) {
        parent::__construct($context);
    }

    public function _prepareLayout()
    {
        $this->pageConfig->getTitle()->set(__('Simple Custom Module'));
        return parent::_prepareLayout();
    }
}

My/Module/Controller/index/contact.php

<?php

namespace My\Module\Controller\Index;

use Magento\Framework\App\Action\Context;
use My\Module\Model\SaveFactory;
class contact extends \Magento\Framework\App\Action\Action
{
    /**
     * @var Save
     */
    protected $_save;

    public function __construct(
        Context $context,
        SaveFactory $save
    ) {
        $this->_contact = $save;
        parent::__construct($context);
    }
    public function execute()
    {
        $save = $this->_contact->create();
        $save->setData($data);
        if($save->save()){
             $this->messageManager->addSuccessMessage(__('You saved the data.'));
        }else{
             $this->messageManager->addErrorMessage(__('Data was not saved.'));
        }
        $resultRedirect =  $this->resultRedirectFactory->create();
        $resultRedirect->setPath('mymodule/index/contact');
        return $resultRedirect;
    }
}

My/Module/etc/module.xml

<?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="My_Module" setup_version="1.1.0" />
</config>

My/Module/etc/frontend/routes.xml

<?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="mymodule" frontName="mymodule">
            <module name="My_Module"/>
        </route>
    </router>
</config>

My/Module/Model/Save.php

<?php 
namespace My\Module\Model;

class Save extends \Magento\Framework\Model\AbstractModel
{
    public function _construct(){
        $this->_init("My\Module\Model\ResourceModel\Save");
    }
}
?>

My/Module/Model/ResourceModel/Save.php

<?php 
namespace My\Module\Model\ResourceModel;
class Save extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
    public function _construct(){
        $this->_init("my_module_post","id");
    }
}
?>

My/Module/Model/ResourceModel/Module/Collection.php

<?php 
namespace My\Module\Model\ResourceModel\Save;
class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{
    public function _construct(){
        $this->_init("My\Module\Model\Save","My\Module\Model\ResourceModel\Save");
    }
}
?>

My/Module/view/registration.php

<?php
    \Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'My_Module',
    __DIR__
    );

My/Module/view/frontend/layout/mymodule_index_contact.xml

<?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>HTML title - The contact form page</title>
    </head>
    <body>
        <referenceBlock name="navigation.sections" remove="true" />
        <referenceContainer name="content">
            <block class="My\Module\Block\Contact" name="mymodule_contact" template="My_Module::contact.phtml" />
        </referenceContainer>
    </body>
</page>       

My/Module/view/frontend/templates/contact.phtml

<h1>Contact page</h1>
<form class="" action="<?php echo $block->getBaseUrl().'mymodule/index/contact'; ?>" method="post">
    <input name="firstname" placeholder="Firstname" type="text">
    <input name="lastname" placeholder="Lastname" type="text">
    <input name="phone" placeholder="Phone Number" type="text">
    <input name="email" placeholder="Email" type="email">
    <input type="submit" value="Send contact informations">
</form>

AND URL IS http://192.168.1.107/magento231/mymodule/index/contact

1

You need to change 2 files and add one more file to save data. Follow the below instruction to get result.

Change the file:

My/Module/Controller/Index/Contact.php

with content:

<?php
namespace My\Module\Controller\Index;

class Contact extends \Magento\Framework\App\Action\Action
{
    protected $_pageFactory;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $pageFactory
    ){
        $this->_pageFactory = $pageFactory;
        return parent::__construct($context);
    }

    public function execute()
    {
        return $this->_pageFactory->create();
    }
}

And file:

My/Module/view/frontend/templates/contact.phtml

with content:

<h1>Contact page</h1>

<form class="" action="<?php echo $block->getBaseUrl().'mymodule/index/save'; ?>" method="post">
<input name="firstname" placeholder="Firstname" type="text">
<input name="lastname" placeholder="Lastname" type="text">
<input name="phone" placeholder="Phone Number" type="text">
<input name="email" placeholder="Email" type="email">
<input type="submit" value="Send contact informations">
</form>

And add one more file:

My/Module/Controller/Index/Save.php

with content:

<?php
namespace My\Module\Controller\Index;

use Magento\Framework\App\Action\Context;
use My\Module\Model\SaveFactory;

class Save extends \Magento\Framework\App\Action\Action
{
    /**
     * @var Save
     */
    protected $_save;

    public function __construct(
        Context $context,
        \Magento\Framework\App\Request\Http $request,
        SaveFactory $save
    ) {
        $this->request = $request;
        $this->_contact = $save;
        parent::__construct($context);
    }

    public function execute()
    {
        $data = $request = $this->request->getParams();
        $save = $this->_contact->create();
        $save->setData($data);
        if($save->save()){
             $this->messageManager->addSuccessMessage(__('You saved the data.'));
        }else{
             $this->messageManager->addErrorMessage(__('Data was not saved.'));
        }
        $resultRedirect =  $this->resultRedirectFactory->create();
        $resultRedirect->setPath('mymodule/index/contact');
        return $resultRedirect;
    }
}

Now run di:compile and other required commands and test.

  • Thanks, Its Worked very well , Can you explain me what we have done here, I am new to Magento 2 – Hardik Radadiya Jun 17 at 10:33
  • 1
    I have created separate controller class for save action and form page url. And in the form page url simply call the page and in the save action gettting post data by http request and save that to the table through your Model class. – Sukumar Gorai Jun 17 at 10:35
  • Okay ,Thank you for Support – Hardik Radadiya Jun 17 at 10:39
  • Welcome and Happy coding! – Sukumar Gorai Jun 17 at 10:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.