gpt4 book ai didi

javascript - 如何将数据从 javascript 发送到 PHP 脚本

转载 作者:行者123 更新时间:2023-11-30 00:36:23 24 4
gpt4 key购买 nike

我在 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);
}
});

并将它们放入 PHP 文件中:

if(isSet($_GET['lat'])){
echo "param good";
} else {
echo "incorrect";
}

当我print_r它时,它总是显示空数组。你能帮我完成这个转换吗?

这是我的更多代码,这是我标记特定纬度和经度的地方:

 <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>

这里我将信息发送到 php 文件:

<input type="submit" id = "ff" value="Send information">

最佳答案

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);

这是一种极其简单的方法,如果您想要有关错误验证等的所有功能,我强烈建议您使用 jquery 和 $.get:

$.ajax({
type: "GET",
data: "lat=" + lat,
url:"address.php",
success: function(data) { console.log(data) }
})

关于javascript - 如何将数据从 javascript 发送到 PHP 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22142309/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com