how to make address fields autocomplete

Hi,

I am trying to make city, state, country autocomplete according to postal code.

i have written a on change JavaScript  but its not work.

i think  the function is not able to call php file.

This my code..please help me

$('#primary_address_postalcode').change(function(){
var pin = $(this).val();

$.ajax({
type:'POST',
url:'SugarCE/SugarCE2/ajaxData.php',
data:'pin='+pin,
dataType: 'json',
success:function(data){

alert(data);

document.getElementById('primary_address_city').value =data[1];
document.getElementById('primary_address_state').value =data[2];
document.getElementById('primary_address_country').value =data[3];
},
error: function(msg)
{
alert(" Error ");
}
});

});

Thank You in Advance

Parents Reply Children
  • Hello Alan,

    I have solved this problem in this way.

    $('#primary_address_postalcode,#billing_address_postalcode,#shipping_address_postalcode,#alt_address_postalcode').on('change', function(){
                                var pin = $(this).val();
                                var idd=$(this).attr("id");
                                var fstid= idd.split("_");
                                  
                                    $.ajax({
                                        type:'POST',
                                        url:'index.php?entryPoint=ajax_test&passdate=$passdate',
                                        data:'pin='+pin,
                                        
                                        success:function(data){
                                           
                                           var ar=data.split("#");
                                            var dst=ar[0];
                                            var st=ar[1];
                                            var cnt=ar[2];
                                           
                                           document.getElementById(fstid[0]+'_address_state').value = st;
                                           document.getElementById(fstid[0]+'_address_country').value = cnt;                                                                 
                                            document.getElementById(fstid[0]+'_address_district_c').value = dst;
                                            
                                        },
                                        error: function(msg)
                                          {
                                              alert("Error while fetching");
                                          }
                                    });      
                  });

    Actually that time i have problem with Url and data type: json so i changed the url   and removed the json . i manually pass all variable and split it.

    Thank You.