我有一个现有的数据库,其中包含城市、州、zip、lat、long、县:40,000多条记录。
我可以毫无问题地使用它。
我现在要做的是,当用户在表单中输入邮政编码时,查询数据库并获得相关的城市,州,州,州,州长和县。
脚本执行"ONBLUR",但什么也没发生。我验证了我调用的函数是正确的,因为我插入了一个window.alert("Test")
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];
}
}
我的"zip_check.php"文件看起来像这样....当我手动查询它时,zip_check工作…
include_once("../db.php");
$query = "SELECT * FROM `cities_extended` WHERE `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'];
我需要包括像JQuery或其他东西,我错过了这个工作吗?我的语法正确吗?
正如@ elclars所说,您在function updateCityState()
中有冗余{。其次,从服务器端发回的数据类型是什么?Json, xml,文本或html,这些信息非常重要,你必须小心使用。通过实现AJAX方法而不是通过jQuery等库使用它,您必须确保定义正确的响应数据类型,处理响应状态并与多个浏览器兼容。下面的代码片段就是一个例子:
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
以这种方式调试并不容易,如果你仍然想保留这个实现,你可以参考这个。就我个人而言,我建议您使用jQuery来完成您的业务。
考虑使用jQuery。Ajax函数,它为您完成了所有艰苦的工作!请参阅官方API文档:http://api.jquery.com/jQuery.ajax/。你的jQuery代码可能看起来像这样:
function updateCityState()
{
var zipValue = $('#zipcode').val();
if(zipValue == "")
{
alert('enter a 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 successfully found your zip code." + data );
//do something with your data here...
$('#city').val(data.city);
$('#state').val(data.state);
});
zipcodeRequest.fail(function(jqXHR, textStatus)
{
alert( "We could not find your zip code (" + textStatus + ")." );
});
}
}
下面是一些ajax代码示例,可以帮助您理解:
- http://www.jquery4u.com/demos/ajax/
- http://www.jquery4u.com/function-demos/ajax/
如果你不想使用jquery,试着使用这个,它是简单的ajax,但与你的实现不同
function updateCityState()
{
var xmlhttp;
var zipValue = document.getElementById('zipcode').value;
if (zipValue=="")
{
var city = document.getElementById('city');
var state = document.getElementById('state');
city.value = "";
state.value = "";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
citystatearr = xmlhttp.responseText.split(",");
var city = document.getElementById('city');
var state = document.getElementById('state');
city.value = citystatearr[0];
state.value = citystatearr[1];
}
}
xmlhttp.open("GET","admin/includes/zip_check.php?zip="+zipValue,true);
xmlhttp.send();
}
尝试使用一个精确的路径到你的php文件。
var url = "admin/includes/zip_check.php";
转换成:
var url = "http://mysite.com/admin/includes/zip_check.php";