2

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

6
  • Please post the code which you have tired
    – Amit Bera
    Commented Mar 11, 2019 at 7:44
  • check my updated question.
    – Amy
    Commented Mar 11, 2019 at 7:58
  • You donot have country field means you want to implement region for a particular country?
    – Amit Bera
    Commented Mar 11, 2019 at 8:09
  • Yes, simply I want to show the region dropdown and save in db.
    – Amy
    Commented Mar 11, 2019 at 8:59
  • you donot get any question, every country has it own regions ,So my question os that you want to show all country region on this dropdown?>
    – Amit Bera
    Commented Mar 11, 2019 at 9:16

1 Answer 1

0

If you want to get a particular country region list at dropdown using getRegionHtmlSelect then you have to do some code.

First, you have to remove class injection like \Magento\Directory\Block\Data $directoryBlock, from __construct().

and you have to pass country id to \Magento\Directory\Block\Data $directoryBlock

Block class code may be look like:

<?php
namespace StackExchange\Magento\Block;

class ContactForm extends \Magento\Contact\Block\ContactForm {
    protected $directoryBlock;
    public function __construct(
            \Magento\Framework\View\Element\Template\Context $context, 
            array $data = array()) 
    {
        parent::__construct($context, $data);
    }
    protected function getDirectoryBlock()
    {
        if(null === $this->directoryBlock)
        {
            $this->directoryBlock = $this->getLayout()
                    ->createBlock('Magento\Directory\Block\Data','contact.directoy')
                    ->getCountryId('US');
        }
        return $this->directoryBlock;
    }
    /**
     *  Getting Region drop down
     * @return string
     */
    public function  getRegionHtml()
    {
        if($this->getDirectoryBlock())
        {
            return $this->getDirectoryBlock()->getRegionHtmlSelect();
        }
        return '';
    }
    public function  getCountryHtml()
    {
        if($this->getDirectoryBlock())
        {
            return $this->getDirectoryBlock()->getCountryHtmlSelect($defValue = null,
                    $name = 'country_id',
                    $id = 'country',
                    $title = 'Country');
        }      
      return '';   
    } 


}

At phtml file, you have to add below code Magento\Directory\Block\Data for getting region drop down

<?= $block->getRegionHtml() ?>

as per as code of field name would be region and html id state

And country html

<?= $block->getCountryHtml() ?>

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.