my custom form is here please help me how to submit form data in custom table in magento2.3 without using objectmanager
-
Follow this link: How to Create Custom Form in Magento2 Frontend?samurai_code– samurai_code2019-05-19 01:37:55 +00:00Commented May 19, 2019 at 1:37
-
Follow this link: How to Create Custom Form in Magento2 Frontend?samurai_code– samurai_code2019-05-19 04:30:05 +00:00Commented May 19, 2019 at 4:30
-
You must create controller to get submitted data in it.Jinesh– Jinesh2019-05-20 05:36:39 +00:00Commented May 20, 2019 at 5:36
-
Review magento.stackexchange.com/a/222428/45214 on how to create a form and save that data in database in magento 2.Kazim Noorani– Kazim Noorani2019-05-20 05:41:14 +00:00Commented May 20, 2019 at 5:41
Add a comment
|
3 Answers
Create a controller in your Vendor/Modlue/Controller/Index
<?php
namespace Vendor\Module\Controller\Index;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\App\Action\Context;
use Vendor\Module\Model\YourModel;
class Blog extends \Magento\Framework\App\Action\Action
{
protected $model;
protected $date;
protected $customermodel;
public function __construct(
Context $context,
YourModel $model,
\Magento\Framework\Stdlib\DateTime\DateTime $date
)
{
$this->model = $model;
$this->date = $date;
parent::__construct($context);
}
public function execute()
{
$query['question'] = $this->getRequest()->getPostValue("question");
$query['email'] = $this->getRequest()->getPostValue("email");
$query['product_name']=$this->getRequest()->getPostValue("product_name");
$query['cutomer_name']=$this->getRequest()->getPostValue("customer_name");
if ($query){
$this->model->setData($query);
$this->model->Save();
$redirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
$redirect->setUrl($this->_redirect->getRefererUrl());
$this->messageManager->addSuccess(__('You submitted your Question successfully.'));
return $redirect;
}
else{
$redirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
$redirect->setUrl($this->_redirect->getRefererUrl());
$this->messageManager->addError (__('Failed To Submit Please Provide Valid Data.'));
return $redirect;
}
}
}
How to create custom forms in Magento 2:
- Step 1: Enable Custom Form module and go to Manage Forms field
- Step 2: Create a new custom form
- Step 3: Preview form in the frontend
That's it.
You must create a controller and then paste the below code in it and do not forgot to change the vendor and mdoulename with your own module name. There should be a model file as well.
namespace Vendor\modulename\Controller\Index;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use vendor\modulename\Model\ExtensionFactory;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\App\Action\Action;
class Submit extends Action
{
protected $resultPageFactory;
protected $extensionFactory;
public function __construct(
Context $context,
PageFactory $resultPageFactory,
ExtensionFactory $extensionFactory
)
{
$this->resultPageFactory = $resultPageFactory;
$this->extensionFactory = $extensionFactory;
parent::__construct($context);
}
public function execute()
{
try {
$data = (array)$this->getRequest()->getPost();
if ($data) {
$model = $this->extensionFactory->create();
$model->setData($data)->save();
$this->messageManager->addSuccessMessage(__("Data Saved Successfully."));
}
} catch (\Exception $e) {
$this->messageManager->addErrorMessage($e, __("We can\'t submit your request, Please try again."));
}
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
$resultRedirect->setUrl($this->_redirect->getRefererUrl());
return $resultRedirect;
}
}
