PHP Magento Tips

Powered by Blogger.

Add Default Country State Dropdown to any Form in Magento

Rate this posting:

It is really very to add default country state drop down to any form.

Let’s start by adding default country state drop down in Contact form page.

Step: 1

First copy Contact's form.phtml file in your template file.

in app/design/frontend/yourtemplate/default/template/contacts/form.phtml add below code:


            $countryList = Mage::getResourceModel('directory/country_collection')





                <label for="country"><?php echo Mage::helper('contacts')->__('Country') ?></label>

                <div class="input-box">

                    <select name="country" onchange="getstate(this.value);">

                        <?php foreach ($countryList as $country) { ?>

                            <option value="<?php echo $country['value']; ?>"><?php echo $country['label']; ?></option>

                        <?php } ?>





                <label for="state"><?php echo Mage::helper('contacts')->__('State') ?></label>

                <div class="input-box" id='statediv'>

                    <input name="state" id="state" title="<?php echo Mage::helper('contacts')->__('State') ?>" value="" class="input-text" type="text" />



Change Mage::helper('contacts') with Mage::helper('modulename') your module name, if you are using this code in your custom extension.

Step 2:

Copy app/code/core/Mage/Contacts/controllers/IndexController.php to app/code/local/Mage/Contacts/controllers/IndexController.php and create new action in it. 

    public function stateAction() {
        $countrycode = $this->getRequest()->getParam('country');
        $html = "";
        $statearray = Mage::getModel('directory/region')->getResourceCollection()->addCountryFilter($countrycode)->load();
        if(count($statearray) > 0){
            $html .= "<select name='state'><option value=''>--Please Select--</option>";
            foreach ($statearray as $_state) {
                $html .= "<option value='" . $_state->getCode() . "'>" . $_state->getDefaultName() . "</option>";
            $html .= "</select>";
        } else {
            $html .= "<input name='state' id='state' title='".Mage::helper('contacts')->__('State')."' value='' class='input-text' type='text' />";
        echo $html;

Note: if you are using this code in your custom extension then Create new action in appropriate module controller and also don't forget to Change Mage::helper('contacts') with Mage::helper('modulename') your module name.

Step 3:

Add below javascript at the end of form.

<script type="text/javascript">
function getstate(countryValue) {
    var reloadurl = '<?php echo $this->getUrl('contacts/index/state'); ?>' + 'country/' + countryValue;
        url: reloadurl, //Relative or absolute path to response.php file
        method: 'get',
        success: function (data) {

Step 4:

if you want State and Country data in your email template then open app/locale/en_US/template/email/contact_form.html file and add below code.

Country: {{var}}
State: {{var data.state}}

Note: if you are using this code in your custom extension then copy above code in your custom module's email template file.

Hope this post helps you.
Tag : Magento
2 Comments On "Add Default Country State Dropdown to any Form in Magento"

The blog was absolutely fantastic! Lot of great information which can be helpful in some or the other way. Keep updating the blog, looking forward for more contents...Great job, keep it up..
ecommerce website development company | ECommerce Development Company in Bangalore|Ecommerce Website Design and Development Company in Bangalore | Magento Development Company


Thank you for sharing useful article

I would like to introduce one of Our web designers and developers will give the best and high quality local USA website Development Services with low cost if you need more details about our web designing services so please feel free to contact us.

Ecommerce Web Development Services in USA

Magento Ecommerce Website Development
Travel portal Development
Web Application Development USA
Yalavarthi Software solutions USA

Back To Top