Add country and state Dropdown in magento admin

How to Add country and state Dropdown in magento admin

In this post we will show you How to Add country and state Dropdown in magento admin. haer in following code will you we how to add state and country in magento admin.

we did not ready to make this in appropriate way like magento does yet in the event that we will have all the condition of all nation then I think this is the ideal arrangement. In the event that we don’t have all state for all nation then this module is not tackled over issue. On the off chance that any one knows the right way then please include the arrangement by means of remark.

Open over form which is in YourNameSpace/YourModuleName/Block/Adminhtml/YourModuleName/Edit/Tab/Form.php then add below fields

// code for country data
$country_data = $fieldset->addField('country', 'select', array(
			'label'     => 'Country',
			'name'  => 'country',	
			'onchange' => 'getstate(this)',					
			'values'    => Mage::getModel('adminhtml/system_config_source_country') ->toOptionArray(),					

// code for field set
$fieldset->addField('state', 'select', array(
			'label'     => 'State',
			'name'  => 'state',
			'values'    => Mage::getModel('YourModuleName/YourModuleName')

 * Add this Ajax for select Country box in html output
 * Add country and state Dropdown
$country_data->setAfterElementHtml("<script type=\"text/javascript\">
	function getstate(selectElement){
		var urlreload = '". $this
		 ->getUrl('YourModuleName/adminhtml_YourModuleName/state') . "country/' + selectElement.value;
		new Ajax.Request(urlreload, {
			// get method for ajax call
			method: 'get',
			onLoading: function (stateformdata) {
				// get method for ajax call
			onComplete: function(stateformdata) {

Now we Create State Action in YourModuleNamecontroller.php file which will be following this

public function stateAction() {
	// get data for country
	$countrycode = $this->getRequest()->getParam('country');
	// option text for country
	$state_data = "<option value=''>Please Select Country</option>";
	if ($country_code != '') {
		// get adat form statearray_val
		$statearray_val = Mage::getModel('directory/region')->getResourceCollection() ->addCountryFilter($country_code)->load();
		foreach ($statearray_val as $_stateVal) {
			// option text for country
			$state_data .= "<option value='" . $_stateVal->getCode() . "'>" . $_stateVal->getDefaultName() . "</option>";
	// echo state data
	echo $state_data;

You also like ::magento product from category id

Leave a Comment

Your email address will not be published. Required fields are marked *

4  +  2  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US