我有这个js做反向地理编码,效果很好。我现在需要使用AJAX将txt变量发送到PHP,但什么都没有,这是我的js,它是通过按钮点击调用的。我知道js工作,因为它将结果返回到我的html页面中的div。
<?php
// Start XML file, create parent node
$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);
$lat = $lng = $address = "";
$servername = "localhost";
$username = "";
$password = "";
$dbname = "";
// Gelocation - get current location
$currPostCode = $_POST['post_code'];
// Opens a connection to a MySQL server
$connection=mysqli_connect($servername, $username, $password, $dbname);
if (!$connection) { die('Not connected : ' . mysqli_error());}
// Select all the rows in the markers table
$query = "SELECT * FROM tblProperties WHERE propertyPostCode = '$currPostCode';";
$result = mysqli_query($connection, $query);
if (!$result) {
die('Invalid query: ' . mysqli_error($connection));
}
header("Content-type: text/xml");
// Iterate through the rows, adding XML nodes for each
while ($row = @mysqli_fetch_assoc($result)){
$lat = $lng = $address = "";
$address = $row['propertyNumber'];
$address .= " ";
$address .= $row['propertyStreet'];
$address .= ", ";
$address .= $row['propertyPostCode'];
// ADD TO XML DOCUMENT NODE
$node = $dom->createElement("marker");
$newnode = $parnode->appendChild($node);
$newnode->setAttribute("name",$row['customerName']);
$newnode->setAttribute("address", $address);
$newnode->setAttribute("lat", $lat);
$newnode->setAttribute("lng", $lng);
$newnode->setAttribute("ID_Status", $row['ID_Status']);
}
echo $dom->saveXML();
echo $currPostCode;
?>
我的php文件没有从POST获得任何内容,或者查询中至少没有返回任何内容,我做错了什么?...
std::move
答案 0 :(得分:0)
首先尝试检查一下是否收到数据:
var d1 = new Date();
var cache = d1.getTime();
$.ajax({
url: "genMapXML.php?cache=" + cache,
type: "post",
data: {post_code: txt},
success: function (data) {
alert(data);
},
error: function () {
alert('failure');
}
});
答案 1 :(得分:0)
以下是代码段:
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script type="text/javascript">
function getLocation()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(reverseGeoLookup);
}
else
{
x.innerHTML="Geolocation is not supported by this browser.";
}
}
function reverseGeoLookup(position) {
var lat = position.coords.latitude;
var lon = position.coords.longitude;
var txt;
var req = new XMLHttpRequest()
req.open("GET", "https://maps.googleapis.com/maps/api/geocode/json?latlng="+lat+","+lon+"&sensor=true", true)
req.onreadystatechange = function() {
if(req.readyState == 4) {
var result = JSON.parse(req.response).results
for(var i = 0, length = result.length; i < length; i++) {
for(var j = 0; j < result[i].address_components.length; j++) {
var component = result[i].address_components[j]
if(~component.types.indexOf("postal_code")) {
txt = component.long_name;
$.ajax({
url: "genMapXML.php",
type: "post",
data: { post_code: txt },
success: function(){
alert('success');
},
error: function(){
alert('failure');
}
});
document.getElementById("trace_location").innerHTML = txt;
return false;
}
}
}
}
}
req.send()
}
getLocation();
</script>
</head>
<body>
<div id="trace_location"></div>
</body>
</html>
在你的php页面中首先有请求变量:
<?php
echo $_REQUEST['post_code'];
// write the required code after having it as a response and do the necessary tasks after that.
?>
希望这能解决您的问题。感谢。