Skip to content
Advertisement

Jquery ajax get request

I am new to jquery and php, I have two input fields, zip and city, the city shall output a value based from the zip that the user input. The jquery script shall call a URL: http://domain.com/city?zip.php=“zip; so that zip.php will return an echo value that will output to the city input field.

I tried using ajax getXMLHTTP. some times it works but sometimes not

Please Refer to the following code snippet below:

 <input type="text" id="zip_code" name="zip_code" /> 

 <input type="text" id="city" name="city" />

 <script type="text/javascript">

 // Some Jquery code here for ajax get request to http://domain.com/city?zip.php

 </script>

Advertisement

Answer

Use jQuery.get, documented here. In the success handler, use the data argument to populate the city input.

Sample:

$.get('http://domain.com/city.php?zip='+$('#IdOfZipInput').val(), function (data){
    $('#IdOfCityInput').val(data);
});
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement