I like to add custom form with submit button. After submitting the form I need to get details to my email. Please let me know how to start.
Thanks in advance.
Okay i created Returns/Cform/registration.php
php?
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Returns_Cform',
__DIR__
);
Returns/Cform/Controller/Index/index.php
namespace Returns\Cform\Controller\Index; use Magento\Framework\App\Action\Action; class Index extends Action { public function __construct( \Magento\Framework\App\Action\Context $context ) { parent::__construct($context); } public function execute() { $post = $this->getRequest()->getPostValue(); $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $data = $objectManager->create('Returns\Cform\Model\Cform'); $data->setData($post); $data->save(); echo "hello"; exit; $this->messageManager->addSuccess(__('Form successfully submitted')); } }
Returns/Cform/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="Returns_Cform" setup_version="2.2.0">
</module>
</config>
Returns/Cform/etc/frontend/routes.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd">
<router id="standard">
<route id="cform" frontName="cform">
<module name="Returns_Cform" />
</route>
</router>
</config>
Returns/Cform/Model/Cform.php
<?php
namespace Returns\Cform\Model;
class Cform extends \Magento\Framework\Model\AbstractModel
{
/**
* Initialize resource model
*
* @return void
*/
protected function _construct()
{
$this->_init('Returns\Cform\Model\ResourceModel\Cform');
}
}
Returns/Cform/Model/ResourceModel/Cform.php
<?php
namespace Returns\Cform\Model\ResourceModel;
class Cform extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
protected $storeManager;
public function __construct(
\Magento\Framework\Model\ResourceModel\Db\Context $context,
\Magento\Store\Model\StoreManagerInterface $storeManager,
$connectionName = null
) {
parent::__construct($context, $connectionName);
$this->storeManager = $storeManager;
}
protected function _construct()
{
$this->_init('returns_contect', 'contect_id');
}
}
Returns/Cform/Model/ResourceModel/Cform/Collection.php
<?php
namespace Returns\Cform\Model\ResourceModel\Cform;
use \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
class Collection extends AbstractCollection
{
protected $_idFieldName = \Returns\Cform\Model\Cform::CONTECT_ID;
/**
* Define resource model
*
* @return void
*/
protected function _construct()
{
$this->_init('Returns\Cform\Model\Cform', 'Returns\Cform\Model\ResourceModel\Cform');
}
}
Return/Cform/Setup/InstallSchema.php
<?php
namespace Returns\Cform\Setup;
use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
/**
* @codeCoverageIgnore
*/
class InstallSchema implements InstallSchemaInterface
{
public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
$installer = $setup;
$installer->startSetup();
/**
* Create table 'returns_contect'
*/
$table = $installer->getConnection()->newTable(
$installer->getTable('returns_contect')
)->addColumn(
'contect_id',
\Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
null,
['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
'Contect Id'
)->addColumn(
'name',
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
255,
['nullable' => false],
'Name'
)->addColumn(
'email',
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
255,
['nullable' => false],
'Email Id'
)->addColumn(
'telephone',
\Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
null,
['nullable'=> false],
'Phone Number'
)->addColumn(
'order_id',
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
null,
['nullable'=> false],
'Order ID'
)->addColumn(
'product',
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
null,
['nullable'=> false],
'Product Name'
)->addColumn(
'quantity',
\Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
null,
['nullable'=> false],
'Quantity'
)->addColumn(
'opened',
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
null,
['nullable'=> false],
'Product is opened'
)->addColumn(
'comment',
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
255,
['nullable' => false],
'Reason for Return'
);
$installer->getConnection()->createTable($table);
}
}
Return/Cform/view/frontend/layout/contactform_index_index.xml
<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 class="Returns\ContactForm\Block\Form" name="contactForm" template="Returns_ContactForm::form.phtml">
<container name="form.additional.info" label="Form Additional Info"/>
</block>
</referenceContainer>
</body>
</page>
Return/Cform/view/frontend/templates/form.phtml
<form id="contact-form" class="form-horizontal" method="post" enctype="multipart/form-data" action="<?php echo $this->getUrl("cform/index/index")?>"> <h3><?= $block->escapeHtml(__('Order Information')) ?></h3> <fieldset class="fieldset"> <div class="field name"> <label class="label" for="name"><span><?= $block->escapeHtml(__('Name')) ?></span></label> <div class="control"> <input name="name" id="name" class="input-text" type="text" /> </div> </div> <div class="field email"> <label class="label" for="email"><span><?= $block->escapeHtml(__('Email')) ?></span></label> <div class="control"> <input name="email" id="email" class="input-text" type="email" /> </div> </div> <div class="field telephone"> <label class="label" for="telephone"><span><?= $block->escapeHtml(__('Phone Number')) ?></span></label> <div class="control"> <input name="telephone" id="telephone" class="input-text" type="text" /> </div> </div> <div class="field order_id"> <label class="label" for="order_id"><span><?= $block->escapeHtml(__('Order ID')) ?></span></label> <div class="control"> <input name="order_id" id="order_id" class="input-text" type="text" /> </div> </div> <h3><?= $block->escapeHtml(__('Product Information')) ?></h3> <div class="field product"> <label class="label" for="product"><span><?= $block->escapeHtml(__('Product Name')) ?></span></label> <div class="control"> <input name="product" id="product" class="input-text" type="text" /> </div> </div> <div class="field quantity"> <label class="label" for="quantity"><span><?= $block->escapeHtml(__('Quantity')) ?></span></label> <div class="control"> <input name="quantity" id="quantity" class="input-text" type="text" /> </div> </div> <div class="field comment"> <label class="label" for="comment"><span><?= $block->escapeHtml(__('Reason for Return')) ?></span></label> <div class="control"> <textarea name="comment" id="comment" class="input-text" cols="5" rows="2" ></textarea> </div> </div> <div class="field opened"> <label class="label" for="comment"><span><?= $block->escapeHtml(__('Product is opened')) ?></span></label> <div class="control"> <label class="radio-inline"> <input type="radio" name="opened" value="1"> Yes</label> <label class="radio-inline"> <input type="radio" name="opened" value="0" checked="checked"> No</label> </div> </div> <?= $block->getChildHtml('form.additional.info') ?> </fieldset> <div class="actions-toolbar"> <div class="primary"> <input type="hidden" name="hideit" id="hideit" value="" /> <button type="submit" id="add" title="" class="action submit primary"> <span><?= $block->escapeHtml(__('Request Return')) ?></span> </button> </div> </div> </form> <script> require(['jquery'],function($){ $(document).ready(function(){ $("#add").click(function(){ var customurl = "<?php echo $this->getUrl("cform/index/index") ?>"; $.ajax({ url: customurl, type: "POST", data: $(this).closest('form').serialize(), dataType: "json", success: function(result){ console.log(result); } }); $('#contact-form')[0].reset(); return false; }); }); }); </script>