I have added a select state dropdown field in my custom phtml file. But not able to save that value in database in Magento2.2.5?
/var/www/html/magento2/app/code/Amy/Contactform/Block
<?php
namespace Amy\Contactform\Block;
use Magento\Framework\View\Element\Template;
class ContactForm extends \Magento\Contact\Block\ContactForm
{
public function __construct(
Template\Context $context,
\Magento\Directory\Block\Data $directoryBlock,
array $data = []
)
{
parent::__construct($context, $data);
$this->_isScopePrivate = true;
$this->directoryBlock = $directoryBlock;
}
public function getCountries()
{
$country = $this->directoryBlock->getCountryHtmlSelect();
return $country;
}
public function getRegion()
{
$region = $this->directoryBlock->getRegionHtmlSelect();
return $region;
}
}
/var/www/html/magento2/app/code/Amy/Contactform/etc/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
<preference for="Magento\Contact\Block\ContactForm" type="Amy\Contactform\Block\ContactForm" />
</config>
/var/www/html/magento2/app/code/Amy/Contactform/etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Amy_Contactform" setup_version="2.0.0">
</module>
</config>
/var/www/html/magento2/app/code/Amy/Contactform/registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Amy_Contactform',
__DIR__
);
/var/www/html/magento2/app/code/Amy/Contactform/view/frontend/layout/contact_index_index.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="contactForm">
<action method="setTemplate">
<argument name="template" xsi:type="string">Amy_Contactform::form.phtml</argument>
</action>
</referenceBlock>
</body>
</page>
/var/www/html/magento2/app/code/Amy/Contactform/view/frontend/templates/form.phtml
<?php
$regionList = $block->getRegion();
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
// @codingStandardsIgnoreFile
/** @var \Magento\Contact\Block\ContactForm $block */
?>
<form class="form contact"
action="<?= $block->escapeUrl($block->getFormAction()) ?>"
id="contact-form"
method="post"
data-hasrequired="<?= $block->escapeHtmlAttr(__('* Required Fields')) ?>"
data-mage-init='{"validation":{}}'>
<fieldset class="fieldset">
<legend class="legend"><span><?= $block->escapeHtml(__('Contact Us')) ?></span></legend><br />
<div class="field name required">
<label class="label" for="name"><span><?= $block->escapeHtml(__('FirstName')) ?></span></label>
<div class="control">
<input name="name" id="name" title="<?= $block->escapeHtmlAttr(__('Name')) ?>" value="<?= $block->escapeHtmlAttr($this->helper('Magento\Contact\Helper\Data')->getPostValue('name') ?: $this->helper('Magento\Contact\Helper\Data')->getUserName()) ?>" class="input-text" type="text" data-validate="{required:true}"/>
</div>
</div>
<div class="field lastname required">
<label class="label" for="name"><span><?= $block->escapeHtml(__('LastName')) ?></span></label>
<div class="control">
<input name="lastname" id="last_name" title="<?= $block->escapeHtmlAttr(__('LastName')) ?>" value="<?= $block->escapeHtmlAttr($this->helper('Magento\Contact\Helper\Data')->getPostValue('lastname') ?: $this->helper('Magento\Contact\Helper\Data')->getUserName()) ?>" class="input-text" type="text" data-validate="{required:true}"/>
</div>
</div>
<div class="field email required">
<label class="label" for="email"><span><?= $block->escapeHtml(__('Email')) ?></span></label>
<div class="control">
<input name="email" id="email" title="<?= $block->escapeHtmlAttr(__('Email')) ?>" value="<?= $block->escapeHtmlAttr($this->helper('Magento\Contact\Helper\Data')->getPostValue('email') ?: $this->helper('Magento\Contact\Helper\Data')->getUserEmail()) ?>" class="input-text" type="email" data-validate="{required:true, 'validate-email':true}"/>
</div>
</div>
<div class="field telephone required">
<label class="label" for="telephone"><span><?= $block->escapeHtml(__('Telephone')) ?></span></label>
<div class="control">
<input name="telephone" id="telephone" title="<?= $block->escapeHtmlAttr(__('Phone Number')) ?>" value="<?= $block->escapeHtmlAttr($this->helper('Magento\Contact\Helper\Data')->getPostValue('telephone') ?: $this->helper('Magento\Contact\Helper\Data')->getUserEmail()) ?>" class="input-text" type="text" data-validate="{required:true}"/>
</div>
</div>
<!-- <div class="field telephone">
<label class="label" for="telephone"><span><?= $block->escapeHtml(__('Telephone')) ?></span></label>
<div class="control">
<input name="telephone" id="telephone" title="<?= $block->escapeHtmlAttr(__('Phone Number')) ?>" value="<?= $block->escapeHtmlAttr($this->helper('Magento\Contact\Helper\Data')->getPostValue('telephone')) ?>" class="input-text" type="text" />
</div>
</div> -->
<div class="field address1 required">
<label class="label" for="address1"><span><?= $block->escapeHtml(__('Address1')) ?></span></label>
<div class="control">
<input name="address1" id="address1" title="<?= $block->escapeHtmlAttr(__('Address1')) ?>" value="<?= $block->escapeHtmlAttr($this->helper('Magento\Contact\Helper\Data')->getPostValue('address1')) ?>" class="input-text" type="text" data-validate="{required:true}"/>
</div>
</div>
<div class="field address2 required">
<label class="label" for="address2"><span><?= $block->escapeHtml(__('Address2')) ?></span></label>
<div class="control">
<input name="address2" id="address2" title="<?= $block->escapeHtmlAttr(__('Address2')) ?>" value="<?= $block->escapeHtmlAttr($this->helper('Magento\Contact\Helper\Data')->getPostValue('address2')) ?>" class="input-text" type="text" data-validate="{required:true}"/>
</div>
</div>
<div class="field city required">
<label class="label" for="city"><span><?= $block->escapeHtml(__('City')) ?></span></label>
<div class="control">
<input name="city" id="city" title="<?= $block->escapeHtmlAttr(__('City')) ?>" value="<?= $block->escapeHtmlAttr($this->helper('Magento\Contact\Helper\Data')->getPostValue('city')) ?>" class="input-text" type="text" data-validate="{required:true}"/>
</div>
</div>
<div class="field stateprovince required">
<label class="label" for="stateprovince"><span><?php echo __('State/Province') ?></span></label>
<div class="control">
<?php echo $regionList?>
</div>
</div>
<div class="field zipcode required">
<label class="label" for="zipcode"><span><?= $block->escapeHtml(__('Zipcode')) ?></span></label>
<div class="control">
<input name="zipcode" id="zipcode" title="<?= $block->escapeHtmlAttr(__('Zipcode')) ?>" value="<?= $block->escapeHtmlAttr($this->helper('Magento\Contact\Helper\Data')->getPostValue('zipcode')) ?>" class="input-text" type="text" data-validate="{required:true}"/>
</div>
</div>
<div class="field comment">
<label class="label" for="comment"><span><?= $block->escapeHtml(__('Message')) ?></span></label>
<div class="control">
<textarea name="comment" id="comment" title="<?= $block->escapeHtmlAttr(__('What’s on your mind?')) ?>" class="input-text" cols="5" rows="3"><?= $block->escapeHtml($this->helper('Magento\Contact\Helper\Data')->getPostValue('comment')) ?></textarea>
</div>
</div>
<?= $block->getChildHtml('form.additional.info') ?>
</fieldset>
<div class="actions-toolbar">
<div class="primary">
<input type="hidden" name="hideit" id="hideit" value="" />
<button type="submit" title="<?= $block->escapeHtmlAttr(__('Submit')) ?>" class="action submit primary">
<span><?= $block->escapeHtml(__('Submit')) ?></span>
</button>
</div>
</div>
</form>
<!-- <script>
$(document).on('change','#country',function() {
var param = 'country='+$('#country').val();
$.ajax({
showLoader: true,
url: YOUR_URL_HERE,
data: param,
type: "GET",
dataType: 'json'
}).done(function (data) {
//data.value has the array of regions
});
});
</script> -->
Any help would be appreciated