Magento Add Country and State Dropdown in Magento admin Form

Hi Magento friends today's i'm share how to Add Country and State Dropdown in Magento admin Form in magento 

Magento Add Country and State Dropdown in Magento admin Form

When you have no states for pirticular country the it show a textbox in textbox you can add state name for country.

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)',

    'class' => 'required-entry',

    'required' => true,

        ));


if (Mage::registry('modulename_data')->getData('state')) {

    $fieldset->addField('state', 'text', array(

        'name' => 'state',

        'label' => 'State',

        'class' => 'required-entry',

        'required' => false,

    ));

} else {

    $statevalues = '';

    if (Mage::registry('modulename_data')->getData('store_image')) {

        $regionModel = Mage::getModel('directory/region')->load(Mage::registry('modulename_data')->getData('store_image'));

        $region = $regionModel->getName();

        $statevalues = array(Mage::registry('modulename_data')->getData('store_image') => $region);

    }


    $fieldset->addField('state', 'select', array(

        'name' => 'state',

        'label' => 'State',

        'value' => Mage::registry('modulename_data')->getData('region_id'),

        'values' => $statevalues,

        'required' => false,

    ));

}


$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').replace(stateform.responseText);

                    }

                });

            }

    </script>");


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


public function stateAction() {

    $countrycode = $this->getRequest()->getParam('country');

    if ($countrycode != '') {

        $statearray = Mage::getModel('directory/region')->getResourceCollection()->addCountryFilter($countrycode)->load();

        $state = "<select class='select' name='state' id='state' class='required-entry validate-select'><option value=''>Please Select</option>";

        foreach ($statearray as $_state) {

            $state .= "<option value='" . $_state->getDefaultName() . "'>" . $_state->getDefaultName() . "</option>";

            $yes = 1;

        }

        $state .= "</select>";

    }

}








Deepak Kumar Bind

Success is peace of mind, which is a direct result of self-satisfaction in knowing you made the effort to become the best of which you are capable.

Post a Comment

If you liked this post please do not forget to leave a comment. Thanks

Previous Post Next Post