Magento – How to i submit the form data in custom table magento2

databasemagento-2.1magento2magento2.3

my custom_tab1.phtml
my custom form is here please help me how to submit form data in custom table in magento2.3 without using objectmanager

my custom form is here please help me how to submit form data in custom table in magento2.3 without using objectmanager

Best Answer

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;
        }
    }
}
Related Topic