0

How would I create a secondary contact form in Magento 2.2? I'd like it to have the following fields:

  • Name
  • Phone
  • Email
  • Checkbox 1
  • Checkbox 2
  • Submit button

I have tried following this thread which does something similar, but couldn't get the module to display anything other than a 404 page: Create Custom Form in Magento2 Frontend

Here's the code I have used based on the above example:

app/code/Company/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="companymodule" frontName="companymodule">
            <module name="Company_Module"/>
        </route>
    </router>
</config>


app/code/Company/Module/view/frontend/layout/module_index_booking.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 booking form page</title>
    </head>
    <body>
        <referenceBlock name="navigation.sections" remove="true" />
        <referenceContainer name="content">
            <block class="Company\Module\Block\Booking" name="companymodule.booking" template="Company_Module::booking.phtml"/>
        </referenceContainer>
    </body>
</page>


app/code/Company/Module/Block/Booking.php

<?php

namespace Company\Module\Block;

class Booking 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 '/companymodule/index/booking';
        // here controller_name is index, action is booking
     }
}


app/code/Company/Module/view/frontend/templates/booking.phtml

<h1>Booking 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 booking informations">
</form>


app/code/Company/Module/Controller/Index/Booking.php

<?php

namespace Company\Module\Controller\Index;

use Magento\Framework\Controller\ResultFactory;

class Booking 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('Booking done !');

            // Redirect to your form page (or anywhere you want...)
            $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
            $resultRedirect->setUrl('/companymodule/index/booking');

            return $resultRedirect;
        }
        // 2. GET request : Render the booking page 
        $this->_view->loadLayout();
        $this->_view->renderLayout();
    }
}

I ran the following commands:

php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento cache:flush

But I cannot get the frontend to show when going to .../companymodule/index/booking or .../module/index/booking

EDIT:

Big part of this issue was me not having Company/Module/registration.php and Company/Module/etc/module.xml file in place.

2

1 Answer 1

0

Please change the :

app/code/Company/Module/view/frontend/layout/module_index_booking.xml

to:

app/code/Company/Module/view/frontend/layout/companymodule_index_booking.xml

Update:

app/code/Company/Module/Controller/Index/Booking.php

<?php

namespace Company\Module\Controller\Index;

use Magento\Framework\Controller\ResultFactory;

class Booking extends \Magento\Framework\App\Action\Action
{

    //add contructor
    protected $_resultPageFactory;

    public function __construct(
         Context $context,
         \Magento\Framework\View\Result\PageFactory $resultPageFactory
    )
    {
    $this->_resultPageFactory = $resultPageFactory;
    parent::__construct($context);
    }
    /**
     * 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('Booking done !');

            // Redirect to your form page (or anywhere you want...)
            $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
            $resultRedirect->setUrl('/companymodule/index/booking');

            return $resultRedirect;
        }
        // 2. GET request : Render the booking page 
        $this->_view->loadLayout();
        $this->_view->renderLayout();
    }
}
5
  • Thanks, I've renamed the file, flushed and re-compiled but unfortunately I still get a 404 page on /companymodule/index/booking
    – Jarxberg
    Commented May 29, 2018 at 11:12
  • What you see if you type : yourdomaine.com/companymodule/index/booking ?
    – PЯINCƎ
    Commented May 29, 2018 at 11:29
  • I get my store's 404 page
    – Jarxberg
    Commented May 29, 2018 at 11:46
  • Look my update, you forgot to add the contructor
    – PЯINCƎ
    Commented May 29, 2018 at 12:09
  • I was still getting a 404 error and realised I was missing the registration.php file. I've added it in and I can see the front-end now. I think it was your original answer that I required so I'll mark this as accepted. Thanks for your help.
    – Jarxberg
    Commented May 29, 2018 at 14:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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