[英]how to send data from javascript to a PHP script
My code in js (doesn't work) to transfer data to PHP is: 我在js中的代码(无效)将数据传输到PHP:
function codeAddress() {
var address = document.getElementById('address').value;
geocoder.geocode({ 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
var input = results[0].geometry.location;
var lat = input.lat();
console.log(lat);
/*var lat = input.lat();
var longitude = input.lng();
document.getElementById("wypisz").innerHTML=lat;
request.open("GET", "address.php?lat=" + lat, true);
//var url = "address.php?lat=latitude&lng =longitude";
//document.getElementById("wypisz").innerHTML = results[0].geometry.location.lng;
*/
$.ajax({
type: "GET",
data: "lat=" + lat,
url:"address.php",
success: function(data) { console.log(data) }
})
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
And to get them in the PHP file : 并将它们保存在PHP文件中:
if(isSet($_GET['lat'])){
echo "param good";
} else {
echo "incorrect";
}
When I print_r
it, it always displays empty Array. 当我
print_r
时,它总是显示空数组。 Can you help me make this conversion work? 您能帮我实现这种转换吗?
Here's more of my code, this is the place where I mark specific latitude and longitude: 这是我的更多代码,这是我标记特定纬度和经度的地方:
<div id="panel">
Address :
<input id="address" name = "address" type="textbox" value="Warszawa, Pol">
<input type="button" value="wstaw" onclick="codeAddress()" >
</div>
<div id="map-canvas"></div>
And here I send information to php file: 在这里,我将信息发送到php文件:
<input type="submit" id = "ff" value="Send information">
Try using ajax call like : 尝试使用像这样的ajax调用:
$.ajax({
type:'GET'
url: "address.php?lat="+lat,
success:function(data){
}
});
var req = new XMLHttpRequest();
req.open("GET", "adress.php", true);
req.onreadystatechange = function() {
if(req.readyState != 4 || req.status != 200)
return;
console.log(req.responseText);
};
req.send("lat=" + lat);
This is an extremely simplistic method though and if you want all bells and whistles in regards to error validation etc, I strongly suggest you use jquery and $.get: 但是,这是一种极其简单的方法,如果您想在错误验证等方面一帆风顺,我强烈建议您使用jquery和$ .get:
$.ajax({
type: "GET",
data: "lat=" + lat,
url:"address.php",
success: function(data) { console.log(data) }
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.