7/30/2012

How to Add country and state Dropdown in magento admin

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.

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'     => 'Country',
            'values'    => Mage::getModel('adminhtml/system_config_source_country') ->toOptionArray(),
            'onchange' => 'getstate(this)',
        ));

$fieldset->addField('state', 'select', array(
            'name'  => 'state',
            'label'     => 'State',
            'values'    => Mage::getModel('modulename/modulename')
                            ->getstate('AU'),
        ));

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

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

8 comments:

  1. Hi

    Thanks for the post. It is working, but i need like default in magento., if there is no state in any country than we can show region textbox to add region.

    ReplyDelete
    Replies
    1. I unable to do, that I clearly mentioned in the post. :D

      Delete
  2. Anonymous1/07/2013

    I got the solution but i am unable to post php code :(

    ReplyDelete
    Replies
    1. Send me to my email Address mentioned in the Contacts tab

      Delete
  3. hello ,

    please send me the code on my mail id amit.maurya@sparxtechnologies.com




    Thanks

    ReplyDelete
  4. Hai,

    i got the solution, i cant able to manage edit page in admin panel..region is not showing..

    Thanks

    ReplyDelete
  5. You have writter $ to access the state & country drop down elements, are you using jquery or core javascript hhere please elaborate & explain. I've tried using your code to dynamically fetch data from dbase & set in another drop down from one drop down, request is 200 ok status but data not fetched. Can you assist me ?

    ReplyDelete