Posted on

How to add select state dropdown field in custom phtml form and save in database(frontend) in magento2.2.5?

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 AmyContactformBlock;

use MagentoFrameworkViewElementTemplate;

class ContactForm extends MagentoContactBlockContactForm
{

public function __construct(
TemplateContext $context,
MagentoDirectoryBlockData $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=”MagentoContactBlockContactForm” type=”AmyContactformBlockContactForm” />
</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
MagentoFrameworkComponentComponentRegistrar::register(
MagentoFrameworkComponentComponentRegistrar::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 MagentoContactBlockContactForm $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(‘MagentoContactHelperData’)->getPostValue(‘name’) ?: $this->helper(‘MagentoContactHelperData’)->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(‘MagentoContactHelperData’)->getPostValue(‘lastname’) ?: $this->helper(‘MagentoContactHelperData’)->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(‘MagentoContactHelperData’)->getPostValue(’email’) ?: $this->helper(‘MagentoContactHelperData’)->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(‘MagentoContactHelperData’)->getPostValue(‘telephone’) ?: $this->helper(‘MagentoContactHelperData’)->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(‘MagentoContactHelperData’)->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(‘MagentoContactHelperData’)->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(‘MagentoContactHelperData’)->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(‘MagentoContactHelperData’)->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(‘MagentoContactHelperData’)->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(‘MagentoContactHelperData’)->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

Leave a Reply

Your email address will not be published. Required fields are marked *