First create a file “index.php” at app\code\local\Magemonkeys\Form\Block with below code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<?php namespace Magemonkeys\Form\Block; class Index extends \Magento\Framework\View\Element\Template { protected $directoryBlock; protected $_isScopePrivate; public function __construct( \Magento\Framework\View\Element\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; } public function getCountryAction() { return $this->getUrl('magemonkeys/form/country', ['_secure' => true]); } } |
Now, you need to create another file at app\code\local\Magemonkeys\Form\view\frontend\layout\ with following code and name it as “form_form_index.xml”
1 2 3 4 5 6 7 8 9 10 11 12 |
<?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="Magemonkeys\Form\Block\Index" name="form" template="form/index.phtml"> </block> </referenceContainer> </body> </page> |
Now create “index.phtml” with following code at app\code\local\Magemonkeys\Form\view\frontend\templates\form
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
<?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 app\code\local\Magemonkeys\Form\Controller\Form\ with following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
<?php namespace Magemonkeys\Form\Controller\Form; class Country extends \Magento\Framework\App\Action\Action { protected $resultJsonFactory; protected $regionColFactory; public function __construct( \Magento\Framework\App\Action\Context $context, \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory, \Magento\Directory\Model\RegionFactory $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]); } } |
[crayon-63d96e03bbb40523763601/] Using above fucntion Images can be imported directly from...
Override view block using di.xml and add the below code...
You can check a list of called layout XML for...
Follow the below steps to install and set up PWA...
If you want to remove all leading zero's from order,...
Let our Magento expert connect to discuss your requirement.
We offer Magento
certified developers.
Our Magento clientele
is 500+.
We sign NDA for the
security of your projects.
We’ve performed 100+
Magento migration projects.
Free quotation
on your project.
Three months warranty on
code developed by us.