Hi,
Since magento has no provision for city dropdown, i’ve tried adding a custom code on my persistent/checkout/onepage/billing.phtml, i added this but city doesnt populate the dropdown,, But when i try it on a single html page, it actually dropsdown.
This is my code:
<div class="input-box">
<select id="billing:region_id" name="billing[region_id]" title="<?php echo $this->__('State/Province') ?>" class="validate-select" style="display:none;">
<option value=""><?php echo $this->__('Please select region, state or province') ?></option>
</select>
<script type="text/javascript">
//<![CDATA[
$('billing:region_id').setAttribute('defaultValue', "<?php echo $this->getAddress()->getRegionId() ?>");
//]]>
</script>
<input type="text" id="billing:region" name="billing[region]" value="<?php echo $this->escapeHtml($this->getAddress()->getRegion()) ?>" title="<?php echo $this->__('State/Province') ?>" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('region') ?>" style="display:none;" />
</div>
</div>
<div class="field ">
<label for="billing:city" class="required"><em>*</em><?php echo $this->__('City') ?></label>
<div class="input-box">
<input class="input-box" type="text" name="name">
<script type="text/javascript">
$('select').change(function(){
if(('billing:region_id').val()!="1")
$('.input-box').replaceWith("<input class="input-box" type='text' name='name'>");
else
$('.input-box').replaceWith('<select class="input-box"><option>a</option></select>');
}).change();
</script>
</div>
12 answers
Hi chima oscar,
Currently i see your code. Do you want to change the country , the city will be the dropdown.
please describe more detail.
Best regards,
Kan Lee
Hi Kan Lee,
Thank you for your reply. What i want to achieve is, when i select a particular state(S), the city field changes from text box to select dropdown.
HI chima oscar
You could add this code at the end of the file.
<script type="text/javascript">
function billingSwitchCityField(){
<?php
$address = Mage::getSingleton('checkout/session')->getQuote()->getBillingAddress();
$defaultCity = $address->getCity();
?>
var selectVal = jQuery('#billing\\:region_id option:selected').val();
if(selectVal != "1"){
jQuery("#billing\\:city")
.replaceWith('<select id="billing:city" name="billing[city]" class="required-entry">' +
'<option value="">choose option</option>' +
'</select>');
}else{
jQuery("#billing\\:city")
.replaceWith('<input type="text" class=" input-text required-entry absolute-advice " title="City" value="<?php echo $defaultCity;?>" id="billing:city" name="billing[city]" autocomplete="off">');
}
}
jQuery(document).ready(function(){
billingSwitchCityField();
jQuery('#billing\\:region_id').change(function() {
billingSwitchCityField();
});
})
</script>
Regards,
Kan Lee
Awesome! This works.
Thanks Kan Lee
Finally Kan,
What if i want my condition to be if two or three different states are selected, will i list them this way
if(selectVal == "1" "2" "3")
or do i use an else if then follow the way you did yours
Hi chima oscar
you could use it:
var myarray = ["1","2","3"];
if(jQuery.inArray(selectVal, myarray)) { }
Regards,
Kan Lee
Hi Kan,
Thanks for your reply.
If i do it this way, they’ll all have the same <select> options.
What i want is, if a ==1 (display this options)
else if b == 2 (display this option)
else if c == 3 (display this option)
else (display text field)
Hi chima oscar
You could do such.
Regards,
Kan Lee
Hai kan this is irfan
How can i add cites list in that above code pls help me?
Hi irfan khan
That is a custom work requirement and you should find a freelancer to help you on this or you can submit a custom work service request from our at https://www.ubertheme.com/magento-custom-services/
and our developers will help you to solve this.
Regards,
Mall.
Hi i want to add city dropdown in my user registration filed.
How can i add it
Hi Kiran Mange,
Hi i want to add city dropdown in your user registration filed.
As this need pretty much custom tweaks, please consider our custom work service to get the feature you mentioned. Our standard support can help you with bug-related issues only.
Regards,
Frank