I have a HTML form, and when i fill in a zipcode, i want the city field to be automatically updated to the city that belongs with that zipcode.
Here's my form:
<form method="post" action="">
<input type="text" name="zipcode" id="zipcode">
<input type="text" name="city" id="city">
</form>
Here is my ajax:
$('#zipcode').keyup(function () {
var el = $(this);
if (el.val().length == 4) {
$.ajax({
url: 'get_city.php',
cache: false,
type: "GET",
data: "zipcode=" + el.val(),
success: function (data) {
$('#city').val(data);
}
})
}
});
And here is the PHP
$db = mysql_connect('localhost', 'root', '');
mysql_select_db('testbox_new', $db);
$sql = 'select * from cities where zipcode = "'.$_GET['zipcode'].'"';
$result = mysql_query($sql);
while ($row = mysql_fetch_array($sql)) {
return $row['city_name'];
}
Anyone who knows why this isn't working?
Thx :)