Oh snap! You are using an old version of browser. Update your browser to get new awesome features. Click for more details.

Magento How to Add country and state Dropdown in admin side

I din't able to create this in proper way like magento does but if you will have all the state of all country then I think this is the perfect solution. If you don't have all state for all country then this module is not solved your problem. If any one knows the correct way then please add the solution via comment. I am describing here How I exactly did.

First Time you have see only country drop down ..


When you select country that time load  ajax






And Next Update all select option with ajax

So now ready to start and display country and state in drop down in custom module.
Open your form which is in Yournamespace/Modulename/Block/Adminhtml/Modulename/Edit/Tab/Form.php then add below fields

$country = $fieldset->addField('country', 'select', array(
            'name'  => 'country',
            'label'     => Mage::helper('storelocator')->__('Country'),
            'values'    => Mage::getModel('adminhtml/system_config_source_country')->toOptionArray(),
            'class' => 'required-entry',
            'required' => true,
            'onchange' => 'getstate(this)',
        ));

//Edit action pass your custom table to country id and get state
$storeId = $this->getRequest()->getParam('id');
        if($storeId !=''):
            $editState = $stateCollection = Mage::getModel('storelocator/storelocator')->load($storeId);
            $stateCollection = Mage::getModel('directory/region')->getResourceCollection()->addCountryFilter($editState->getCountry())->load();
            $state = "";
            foreach ($stateCollection as $_state) {
                $state[]= array('value'=>$_state->getCode(),'label'=>$_state->getDefaultName());
            }
            $fieldset->addField('state', 'select', array(
                'label' => Mage::helper('storelocator')->__('State'),
                'required' => false,
                'name' => 'state',
                'selected' => 'selected',
                'values' => $state,
            ));
        else:
            //New action
            $fieldset->addField('state', 'select', array(
                'name'  => 'state',
                'required' => false,
                'label' => Mage::helper('storelocator')->__('State'),
                'values' => '--Please Select Country--',
            ));
        endif;

        /*
        * Add Ajax to the Country select box html output
        */
        $country->setAfterElementHtml("<script type=\"text/javascript\">
            function getstate(selectElement){
                var reloadurl = '". $this->getUrl('storelocator/adminhtml_storelocator/state') . "country/' + selectElement.value;
                new Ajax.Request(reloadurl, {
                    method: 'get',
                    onComplete: function(transport){
                        var response = transport.responseText;
                        $('storelocatorstate').update(response);
                    }
                });
            }
        </script>");


And Now Create State Action in modulenamecontroller.php file which will be like this

public function stateAction() {
        $countrycode = $this->getRequest()->getParam('country');
        $state = "<option value=''>--Please Select--</option>";
        if ($countrycode != '') {
            $statearray = Mage::getModel('directory/region')->getResourceCollection()->addCountryFilter($countrycode)->load();
            foreach ($statearray as $_state) {
                $state .= "<option value='" . $_state->getCode() . "'>" . $_state->getDefaultName() . "</option>";
            }
        }
        echo $state;
    }


It's working in Magento1.7 I already tested. i have use only prototype js code with ajax this code not conflict any js

I Think This Post is Usefully You  :) 

3 comments

  1. Great Read! I am impressed on how you make your article easy to understand. I'll come back for more :D

    Japs Buidon is a Social Media Specialist and SEO from a renowned Magento Development Company in Florida. He loves hiking as well as electronics.

    ReplyDelete
  2. HI,

    Its very useful article and easy to understand, Thanks for share this kind of article.Save my lot of time.
    I'll come back for more.:)

    ReplyDelete