0

I am created one form for contact and i want to insert form data into custom table in database in magento 2.3.5 so can anyone have idea about it then please send me solution.

<?php
namespace Aspl\Contact\Controller\Ajax;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\View\Result\PageFactory;
use Aspl\Contact\Model\ContactFactory;

class ContDet extends Action
{
    protected $_pageFactory;

    protected $_resultPageFactory;

    protected $_resultJsonFactory;
    
    protected $contactFactory;

    public function __construct(
        Context $context, ContactFactory $contactFactory, PageFactory $resultPageFactory, JsonFactory $resultJsonFactory)
    {
       $this->contactFactory = $contactFactory;
       $this->_resultPageFactory = $resultPageFactory;
       $this->_resultJsonFactory = $resultJsonFactory;
       return parent::__construct($context);
    }

    public function execute()
    {
        $result = $this->_resultJsonFactory->create();
        $resultPage = $this->_resultPageFactory->create();
                
        try {
                
                $data = $this->getRequest()->getParams('data');

                $model = $this->contactFactory->create();

                //save data to variables
                $model->setName($data['name']);
                $model->setContact($data['contact']);
                $model->setAddress($data['address']);
                $model->setComments($data['comments']);

                $model->save();
     
                $response = [
                    'errors' => false,
                    'message' => __('Please Wait our Service team can contact you as soon as possible.')
                ];

            } 
            catch (\Exception $e) 
            {
                $response = [
                    'errors' => true,
                    'message' => __('Invalid Contact Details.').$e->getMessage()
                ];
            }
            return $result;
    }
}

Thank you in Advance.

1 Answer 1

1

it will help you to save data in custom table, please refer this blog

9
  • I want to add data using controller and using model,resource model Commented Jun 26, 2020 at 10:44
  • your solution is not working Commented Jun 26, 2020 at 10:44
  • what is error are you facing ?? Commented Jun 26, 2020 at 10:57
  • data is not ging to save in database Commented Jun 26, 2020 at 11:23
  • I can send my controller please reply where is the mistake Commented Jun 26, 2020 at 11:24

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.