We sacrifice by not doing any other technology, so that you get the best of Magento.

We sacrifice by not doing any other technology, so that you get the best of Magento.

    First create a file “index.php” at appcodelocalMagemonkeysFormBlock with below code.

    <?php
    namespace MagemonkeysFormBlock;
     
    class Index extends MagentoFrameworkViewElementTemplate
    {
        protected $directoryBlock;
        protected $_isScopePrivate;
        
        public function __construct(
             MagentoFrameworkViewElementTemplateContext $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;
             }
             public function getCountryAction()
             {
             return $this->getUrl('magemonkeys/form/country', ['_secure' => true]);
             }
        
        
    }

    Now, you need to create another file at appcodelocalMagemonkeysFormviewfrontendlayout with following code and name it as “form_form_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">
             <head>
             <title>form</title>
             </head>
         <body>     
             <referenceContainer name="content">                 
                         <block class="MagemonkeysFormBlockIndex" name="form" template="form/index.phtml">
                         </block>
             </referenceContainer>       
             </body>
    </page>

    Now create “index.phtml” with following code at appcodelocalMagemonkeysFormviewfrontendtemplatesform

    <?php
    $countryList=$block->getCountries();
    $regionList=$block->getRegion();  ?>
    <form class="form retuns"
             action="<?php /* @escapeNotVerified */ echo $block->getFormAction(); ?>"
             id="return-form"
             method="post"
             data-hasrequired="<?php /* @escapeNotVerified */ echo __('* Required Fields') ?>"
             data-mage-init='{"validation":{}}'>
     
             <div class="field country required">
                           <label class="label" for="country"><span><?php /* @escapeNotVerified */ echo __('Country') ?></span></label>
                           <div class="control">
                                       <?php echo $countryList?>
                           </div>
                   </div>
     
             <div class="field region required">
                           <label class="label" for="state"><span><?php /* @escapeNotVerified */ echo __('State') ?></span></label>
                           <div class="control">
                           <?php echo $regionList?>
                           </div>
                           </div>
                           
                           
                           <div class="field states required" style="display:none">
                           <label class="label" for="states"><span><?php /* @escapeNotVerified */ echo __('State') ?></span></label>
                           <div class="control">
                           <input name="state" id="states" title="<?php /* @escapeNotVerified */ echo __('State') ?>"  class="input-text" type="text" />
                           </div>
                           </div>
     
    </form>
     
    <script>
             jQuery(document).on('change','#country',function() {
             var param = 'country='+jQuery('#country').val();
             jQuery.ajax({
                         showLoader: true,
                         url: '<?php /* @escapeNotVerified */ echo $block->getCountryAction(); ?>',
                         data: param,
                         type: "GET",
                         dataType: 'json'
             }).done(function (data) {
                         jQuery('#state').empty();
                         if(data.value=='')
                         {
                                     jQuery('.field.states.required').show();
                                     jQuery('.field.region.required').hide();
                         }
                         else
                         {
                         jQuery('#state').append(data.value);
                                     jQuery('.field.states.required').hide();
                                     jQuery('.field.region.required').show();
                         }
             });
        });
    </script>

    Lastly, you need to create “Country.php” in your form folder at appcodelocalMagemonkeysFormControllerForm with following code.

    <?php
    namespace MagemonkeysFormControllerForm;
     
    class Country extends MagentoFrameworkAppActionAction
    {
             protected $resultJsonFactory;
        
        protected $regionColFactory;
     
             public function __construct(
             MagentoFrameworkAppActionContext $context,
             MagentoFrameworkControllerResultJsonFactory $resultJsonFactory,
             MagentoDirectoryModelRegionFactory $regionColFactory)
        {     
             $this->regionColFactory     = $regionColFactory;
             $this->resultJsonFactory     = $resultJsonFactory;
             parent::__construct($context);
             }
     
             public function execute()
             {
             $this->_view->loadLayout();
             $this->_view->getLayout()->initMessages();
             $this->_view->renderLayout();
     
             $result           = $this->resultJsonFactory->create();
             $regions=$this->regionColFactory->create()->getCollection()->addFieldToFilter('country_id',$this->getRequest()->getParam('country'));
             
             $html = '';
             
             if(count($regions) > 0)
             {
                         $html.='<option selected="selected" value="">Please select a region, state or province.</option>';
                         foreach($regions as $state)
                         {
                                     $html.=    '<option  value="'.$state->getName().'">'.$state->getName().'.</option>';
                         }
             }
             return $result->setData(['success' => true,'value'=>$html]);
       }
    }

     

    field_5bfb909c5ccae

      Get a Free Quote