繁体   English   中英

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

[英]how to send data from javascript to a PHP script

我在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">

尝试使用像这样的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);

但是,这是一种极其简单的方法,如果您想在错误验证等方面一帆风顺,我强烈建议您使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM