PHP Magento Tips

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:

            <?php

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

                    ->loadData()

                    ->toOptionArray(false);

            ?>

            <li>

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

                    </select>

                </div>

            </li>

            <li>

                <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" />

                </div>

            </li> 

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;
    jQuery.ajax({
        url: reloadurl, //Relative or absolute path to response.php file
        method: 'get',
        success: function (data) {
            jQuery('#statediv').html(data);
        }
    });
}
</script> 

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 data.country}}
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.
Thanks.
Tag : Magento
1 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

Back To Top