php - get city and state from zip code entered javascript -


i have existing db contains city, state, zip, lat, long, county: 40,000+ records.

i able use no problems.

what i'm trying when user enters zip code in form, query db , associated city, state, lat, long, , county.

the script executes "onblur" nothing happens. verified i'm calling function right because inserted window.alert("test").

here javascript:

function updatecitystate()    {    {     var zipvalue = document.getelementbyid('zipcode').value;     if(zipvalue!="")     {         var url = "admin/includes/zip_check.php";         var param = "?zip=" + escape(zipvalue);          var ajax = gethttpobject();          ajax.open("get", url + param, true);         ajax.onreadystatechange = handleajax;         ajax.send(null);     } } }  function handleajax() { if (ajax.readystate == 4) {     citystatearr = ajax.responsetext.split(",");      var city = document.getelementbyid('city');     var state = document.getelementbyid('state');      city.value = citystatearr[0];     state.value = citystatearr[1]; }  } 

my "zip_check.php" file looks this.... zip_check works when query manually...

include_once("../db.php"); $query = "select * `cities_extended` `zip`=".mysql_real_escape_string($_get['zip']); $result = mysql_query($query) or die(mysql_error());   $row = mysql_fetch_array($result); echo $row['city'].",".$row['state_code']; 

do need include jquery or else i'm missing work? syntax correct?

consider using jquery.ajax function hard work you! see official api documentation: http://api.jquery.com/jquery.ajax/. jquery code might this:

function updatecitystate() {     var zipvalue = $('#zipcode').val();     if(zipvalue == "")     {         alert('enter zip value!');     }     else     {         //process ajax request         var zipcoderequest = $.ajax({              type: "get",              url: "admin/includes/zip_check.php",              data: { zip:zipvalue }         });          zipcoderequest.done(function(data)         {              alert( "you have found zip code." + data );              //do data here...              $('#city').val(data.city);              $('#state').val(data.state);         });          zipcoderequest.fail(function(jqxhr, textstatus)         {              alert( "we not find zip code (" + textstatus + ")." );         });     } } 

here more ajax code examples figure out:

  1. http://www.jquery4u.com/demos/ajax/
  2. http://www.jquery4u.com/function-demos/ajax/

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -