1

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

3

4 Answers 4

0

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;
        }
    }
}
0

Review https://magento.stackexchange.com/a/222428/45214 on how to create a form and save that data in database in magento 2.

0

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.

0

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;

    }
}

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.