Magento 2.1 – How to Save Custom Field in Custom Database Table While Editing Product from Backend

admin-controllermagento-2.1

I have created a custom module to display custom tab on product form in back-end. I used this solution.

Now on tab i am adding custom fields to save in custom database table.
say <input type="text" name="my_new_field" value="123">

Also created a custom controller for admin product save like below.

In etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Catalog\Controller\Adminhtml\Product\Save" type="Namespace\Module\Controller\Adminhtml\Rewrite\Product\Save" />
</config>

And in Controller/Adminhtml/Rewrite/Product/Save.php

<?php

    namespace Namespace\Module\Controller\Adminhtml\Rewrite\Product;

    class Save extends \Magento\Catalog\Controller\Adminhtml\Product\save
    {

        public function execute()
        {
            echo "hello"; print_r($_POST); die;

            return parent::execute();
        }
    }

Now in execute function i am expecting POST value of my_new_field. But i am not getting it. After getting that i will use custom queries to save data in custom table.

What am i doing wrong or should i use some other method?

Update: 26 Aug.

I have used Ajax form to save data from product tab as i had time restrictions. I have accepted @william-oakley's answer. Now as @mageworx added in his answer that this is not a standard way to do this.

I want to use UI form standard usage in further development.
So my question is how to add custom tab to product edit using UI form standard and save custom fields in custom table or in another way.

Best Answer

You can just use a "naked" input field, you just have to add the following attribute:

data-form-part="product_form"

so:

<input data-form-part="product_form" type="text" name="my_new_field" value="123">

You will then be able to get the POST data for your input.