Thursday 20 March 2014

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

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Why the state action do not response anything?
    I think you should add

    if ( $yes ) die ( $state );

    to response select options data.

    ReplyDelete