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

  • Hi,

    Please use the following code:

    $.ajax({
               url: "index.php",
               type: "POST",
               dataType: "html",
               data: "module=[MODULE-NAME]&action=[ACTION-NAME]&sugar_body_only=true&[PARAM-NAME]=" + [PARAM-VALUE] ,
               async: true,
               cache: false,
               success: function (response)
               {
    console.log(response);
       }
    });

    In SugarCRM CE we call actions not files through ajax. Actions can be created in modules controller or a simple php file in a module directory behave same like an action.
    e.g custom/modules/Contacts/autofillAddress.php

    and we can call this action.

    $.ajax({
               url: "index.php",
               type: "POST",
               dataType: "html",
               data: "module=COntacts&action=autofillAddress&sugar_body_only=true&postal_code=63000" ,
               async: true,
               cache: false,
               success: function (response)
               {
    console.log(response);
       }
    });

    Hope this helps.

    Regards,

    RT

  • Hi Ajit Agarw

    Where and how you are calling your js files.

    Can you check the post and let me know for further help.

    Best Regards

    S Ramana Raju

  • Hello every one,

    Thank you for helping me now i had solved this problem...

  • 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.